Skip to content

Commit 7fa94af

Browse files
committed
Make feature suggestion more consistent.
1 parent c6e77b3 commit 7fa94af

File tree

13 files changed

+40
-87
lines changed

13 files changed

+40
-87
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
463463
);
464464
}
465465

466-
fn crate_inject_span(&self) -> Option<Span> {
467-
self.tcx.hir_crate_items(()).definitions().next().and_then(|id| {
468-
self.tcx.crate_level_attribute_injection_span(self.tcx.local_def_id_to_hir_id(id))
469-
})
470-
}
471-
472466
/// Check the const stability of the given item (fn or trait).
473467
fn check_callee_stability(&mut self, def_id: DefId) {
474468
match self.tcx.lookup_const_stability(def_id) {
@@ -543,7 +537,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
543537
feature,
544538
feature_enabled,
545539
safe_to_expose_on_stable: callee_safe_to_expose_on_stable,
546-
suggestion_span: self.crate_inject_span(),
547540
is_function_call: self.tcx.def_kind(def_id) != DefKind::Trait,
548541
});
549542
}
@@ -919,7 +912,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
919912
name: intrinsic.name,
920913
feature,
921914
const_stable_indirect: is_const_stable,
922-
suggestion: self.crate_inject_span(),
923915
});
924916
}
925917
Some(attrs::ConstStability {

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::{ConstContext, LangItem};
4+
use rustc_errors::Diag;
45
use rustc_errors::codes::*;
5-
use rustc_errors::{Applicability, Diag};
66
use rustc_hir as hir;
77
use rustc_hir::def_id::DefId;
88
use rustc_infer::infer::TyCtxtInferExt;
@@ -384,7 +384,6 @@ pub(crate) struct CallUnstable {
384384
/// expose on stable.
385385
pub feature_enabled: bool,
386386
pub safe_to_expose_on_stable: bool,
387-
pub suggestion_span: Option<Span>,
388387
/// true if `def_id` is the function we are calling, false if `def_id` is an unstable trait.
389388
pub is_function_call: bool,
390389
}
@@ -412,20 +411,7 @@ impl<'tcx> NonConstOp<'tcx> for CallUnstable {
412411
def_path: ccx.tcx.def_path_str(self.def_id),
413412
})
414413
};
415-
// FIXME: make this translatable
416-
let msg = format!("add `#![feature({})]` to the crate attributes to enable", self.feature);
417-
#[allow(rustc::untranslatable_diagnostic)]
418-
if let Some(span) = self.suggestion_span {
419-
err.span_suggestion_verbose(
420-
span,
421-
msg,
422-
format!("#![feature({})]\n", self.feature),
423-
Applicability::MachineApplicable,
424-
);
425-
} else {
426-
err.help(msg);
427-
}
428-
414+
ccx.tcx.disabled_nightly_features(&mut err, [(String::new(), self.feature)]);
429415
err
430416
}
431417
}
@@ -452,7 +438,6 @@ pub(crate) struct IntrinsicUnstable {
452438
pub name: Symbol,
453439
pub feature: Symbol,
454440
pub const_stable_indirect: bool,
455-
pub suggestion: Option<Span>,
456441
}
457442

458443
impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
@@ -472,8 +457,7 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
472457
span,
473458
name: self.name,
474459
feature: self.feature,
475-
suggestion: self.suggestion,
476-
help: self.suggestion.is_none(),
460+
suggestion: ccx.tcx.crate_level_attribute_injection_span(),
477461
})
478462
}
479463
}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ pub(crate) struct UnstableIntrinsic {
136136
code = "#![feature({feature})]\n",
137137
applicability = "machine-applicable"
138138
)]
139-
pub suggestion: Option<Span>,
140-
#[help(const_eval_unstable_intrinsic_suggestion)]
141-
pub help: bool,
139+
pub suggestion: Span,
142140
}
143141

144142
#[derive(Diagnostic)]

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,7 @@ fn default_body_is_unstable(
302302
reason: reason_str,
303303
});
304304

305-
let inject_span = item_did
306-
.as_local()
307-
.and_then(|id| tcx.crate_level_attribute_injection_span(tcx.local_def_id_to_hir_id(id)));
305+
let inject_span = item_did.is_local().then(|| tcx.crate_level_attribute_injection_span());
308306
rustc_session::parse::add_feature_diagnostics_for_issue(
309307
&mut err,
310308
&tcx.sess,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
10641064
Ok(..) => Some(vec![(adt_const_params_feature_string, sym::adt_const_params)]),
10651065
};
10661066
if let Some(features) = may_suggest_feature {
1067-
tcx.disabled_nightly_features(&mut diag, Some(param.hir_id), features);
1067+
tcx.disabled_nightly_features(&mut diag, features);
10681068
}
10691069

10701070
Err(diag.emit())

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ pub(crate) struct BaseExpressionDoubleDot {
3030
)]
3131
pub default_field_values_suggestion: Option<Span>,
3232
#[subdiagnostic]
33-
pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
34-
#[subdiagnostic]
3533
pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
3634
#[subdiagnostic]
3735
pub remove_dots: Option<BaseExpressionDoubleDotRemove>,
@@ -61,10 +59,6 @@ pub(crate) struct BaseExpressionDoubleDotAddExpr {
6159
pub span: Span,
6260
}
6361

64-
#[derive(Subdiagnostic)]
65-
#[help(hir_typeck_base_expression_double_dot_enable_default_field_values)]
66-
pub(crate) struct BaseExpressionDoubleDotEnableDefaultFieldValues;
67-
6862
#[derive(Diagnostic)]
6963
#[diag(hir_typeck_field_multiply_specified_in_initializer, code = E0062)]
7064
pub(crate) struct FieldMultiplySpecifiedInInitializer {

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectatio
4242
use crate::coercion::{CoerceMany, DynamicCoerceMany};
4343
use crate::errors::{
4444
AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
45-
BaseExpressionDoubleDotEnableDefaultFieldValues, BaseExpressionDoubleDotRemove,
46-
CantDereference, FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
47-
HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType, NoFieldOnVariant,
48-
ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
45+
BaseExpressionDoubleDotRemove, CantDereference, FieldMultiplySpecifiedInInitializer,
46+
FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType,
47+
NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
4948
TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
5049
};
5150
use crate::{
@@ -2133,26 +2132,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21332132
}
21342133
}
21352134
if !self.tcx.features().default_field_values() {
2136-
let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
2135+
let sugg = self.tcx.crate_level_attribute_injection_span();
21372136
self.dcx().emit_err(BaseExpressionDoubleDot {
21382137
span: span.shrink_to_hi(),
21392138
// We only mention enabling the feature if this is a nightly rustc *and* the
21402139
// expression would make sense with the feature enabled.
21412140
default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
21422141
&& missing_mandatory_fields.is_empty()
21432142
&& !missing_optional_fields.is_empty()
2144-
&& sugg.is_some()
21452143
{
2146-
sugg
2147-
} else {
2148-
None
2149-
},
2150-
default_field_values_help: if self.tcx.sess.is_nightly_build()
2151-
&& missing_mandatory_fields.is_empty()
2152-
&& !missing_optional_fields.is_empty()
2153-
&& sugg.is_none()
2154-
{
2155-
Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
2144+
Some(sugg)
21562145
} else {
21572146
None
21582147
},

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,6 @@ impl<'tcx> Pick<'tcx> {
17271727
}
17281728
tcx.disabled_nightly_features(
17291729
lint,
1730-
Some(scope_expr_id),
17311730
self.unstable_candidates.iter().map(|(candidate, feature)| {
17321731
(format!(" `{}`", tcx.def_path_str(candidate.item.def_id)), *feature)
17331732
}),

compiler/rustc_middle/src/ty/context.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,42 +3165,33 @@ impl<'tcx> TyCtxt<'tcx> {
31653165
lint_level(self.sess, lint, level, Some(span.into()), decorate);
31663166
}
31673167

3168-
/// Find the crate root and the appropriate span where `use` and outer attributes can be
3169-
/// inserted at.
3170-
pub fn crate_level_attribute_injection_span(self, hir_id: HirId) -> Option<Span> {
3171-
for (_hir_id, node) in self.hir_parent_iter(hir_id) {
3172-
if let hir::Node::Crate(m) = node {
3173-
return Some(m.spans.inject_use_span.shrink_to_lo());
3174-
}
3175-
}
3176-
None
3168+
/// Find the appropriate span where `use` and outer attributes can be inserted at.
3169+
pub fn crate_level_attribute_injection_span(self) -> Span {
3170+
let node = self.hir_node(hir::CRATE_HIR_ID);
3171+
let hir::Node::Crate(m) = node else { bug!() };
3172+
m.spans.inject_use_span.shrink_to_lo()
31773173
}
31783174

31793175
pub fn disabled_nightly_features<E: rustc_errors::EmissionGuarantee>(
31803176
self,
31813177
diag: &mut Diag<'_, E>,
3182-
hir_id: Option<HirId>,
31833178
features: impl IntoIterator<Item = (String, Symbol)>,
31843179
) {
31853180
if !self.sess.is_nightly_build() {
31863181
return;
31873182
}
31883183

3189-
let span = hir_id.and_then(|id| self.crate_level_attribute_injection_span(id));
3184+
let span = self.crate_level_attribute_injection_span();
31903185
for (desc, feature) in features {
31913186
// FIXME: make this string translatable
31923187
let msg =
31933188
format!("add `#![feature({feature})]` to the crate attributes to enable{desc}");
3194-
if let Some(span) = span {
3195-
diag.span_suggestion_verbose(
3196-
span,
3197-
msg,
3198-
format!("#![feature({feature})]\n"),
3199-
Applicability::MaybeIncorrect,
3200-
);
3201-
} else {
3202-
diag.help(msg);
3203-
}
3189+
diag.span_suggestion_verbose(
3190+
span,
3191+
msg,
3192+
format!("#![feature({feature})]\n"),
3193+
Applicability::MaybeIncorrect,
3194+
);
32043195
}
32053196
}
32063197

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,11 +3575,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35753575
}
35763576
ObligationCauseCode::TrivialBound => {
35773577
err.help("see issue #48214");
3578-
tcx.disabled_nightly_features(
3579-
err,
3580-
Some(tcx.local_def_id_to_hir_id(body_id)),
3581-
[(String::new(), sym::trivial_bounds)],
3582-
);
3578+
tcx.disabled_nightly_features(err, [(String::new(), sym::trivial_bounds)]);
35833579
}
35843580
ObligationCauseCode::OpaqueReturnType(expr_info) => {
35853581
let (expr_ty, expr) = if let Some((expr_ty, hir_id)) = expr_info {

tests/ui/consts/const-unstable-intrinsic.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ error: `size_of_val` is not yet stable as a const intrinsic
2424
LL | unstable_intrinsic::size_of_val(&x);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
|
27-
= help: add `#![feature(unstable)]` to the crate attributes to enable
27+
help: add `#![feature(unstable)]` to the crate attributes to enable
28+
|
29+
LL + #![feature(unstable)]
30+
|
2831

2932
error: `align_of_val` is not yet stable as a const intrinsic
3033
--> $DIR/const-unstable-intrinsic.rs:20:9
3134
|
3235
LL | unstable_intrinsic::align_of_val(&x);
3336
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3437
|
35-
= help: add `#![feature(unstable)]` to the crate attributes to enable
38+
help: add `#![feature(unstable)]` to the crate attributes to enable
39+
|
40+
LL + #![feature(unstable)]
41+
|
3642

3743
error: const function that might be (indirectly) exposed to stable cannot use `#[feature(local)]`
3844
--> $DIR/const-unstable-intrinsic.rs:24:9

tests/ui/stability-attribute/const-stability-attribute-implies-no-feature.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error: `foobar` is not yet stable as a const fn
44
LL | foobar();
55
| ^^^^^^^^
66
|
7-
= help: add `#![feature(const_foobar)]` to the crate attributes to enable
7+
help: add `#![feature(const_foobar)]` to the crate attributes to enable
8+
|
9+
LL + #![feature(const_foobar)]
10+
|
811

912
error: aborting due to 1 previous error
1013

tests/ui/traits/const-traits/staged-api-user-crate.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ error: `staged_api::MyTrait` is not yet stable as a const trait
1515
LL | Unstable::func();
1616
| ^^^^^^^^^^^^^^^^
1717
|
18-
= help: add `#![feature(unstable)]` to the crate attributes to enable
18+
help: add `#![feature(unstable)]` to the crate attributes to enable
19+
|
20+
LL + #![feature(unstable)]
21+
|
1922

2023
error: aborting due to 2 previous errors
2124

0 commit comments

Comments
 (0)