diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2025-01-19 11:48:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-19 11:48:15 +0100 |
| commit | 4842ff4a85c9315103885eca2f2fc98bd2ee7826 (patch) | |
| tree | 0d72bf1e59968c8338c248d6d59a88cdaac29899 /compiler | |
| parent | c62b732724b6beb50a5c73be26d0c5f668504059 (diff) | |
| parent | e68a8ce0350892d97ff24323bf98771d65d0ed52 (diff) | |
| download | rust-4842ff4a85c9315103885eca2f2fc98bd2ee7826.tar.gz rust-4842ff4a85c9315103885eca2f2fc98bd2ee7826.zip | |
Rollup merge of #134858 - estebank:issue-81370, r=Noratrieb
Provide structured suggestion for `#![feature(..)]` in more cases Fix #81370.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_const_eval/messages.ftl | 2 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/check_consts/check.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/check_consts/ops.rs | 18 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/errors.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_hir_typeck/src/errors.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_hir_typeck/src/expr.rs | 13 |
6 files changed, 54 insertions, 7 deletions
diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index 4861b7a4430..d4bfb781320 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -424,7 +424,7 @@ const_eval_unstable_in_stable_exposed = .bypass_sugg = otherwise, as a last resort `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (this requires team approval) const_eval_unstable_intrinsic = `{$name}` is not yet stable as a const intrinsic - .help = add `#![feature({$feature})]` to the crate attributes to enable +const_eval_unstable_intrinsic_suggestion = add `#![feature({$feature})]` to the crate attributes to enable const_eval_unterminated_c_string = reading a null-terminated string starting at {$pointer} with no null found before end of allocation diff --git a/compiler/rustc_const_eval/src/check_consts/check.rs b/compiler/rustc_const_eval/src/check_consts/check.rs index 06d6a5a5af5..ed34996a7a7 100644 --- a/compiler/rustc_const_eval/src/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/check_consts/check.rs @@ -464,6 +464,12 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { err_span, ); } + + fn crate_inject_span(&self) -> Option<Span> { + self.tcx.hir_crate_items(()).definitions().next().and_then(|id| { + self.tcx.crate_level_attribute_injection_span(self.tcx.local_def_id_to_hir_id(id)) + }) + } } impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { @@ -809,6 +815,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { name: intrinsic.name, feature, const_stable_indirect: is_const_stable, + suggestion: self.crate_inject_span(), }); } Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => { @@ -897,7 +904,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { // regular stability. feature == sym::rustc_private && issue == NonZero::new(27812) - && self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked + && tcx.sess.opts.unstable_opts.force_unstable_if_unmarked }; // Even if the feature is enabled, we still need check_op to double-check // this if the callee is not safe to expose on stable. @@ -907,6 +914,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { feature, feature_enabled, safe_to_expose_on_stable: callee_safe_to_expose_on_stable, + suggestion_span: self.crate_inject_span(), }); } } diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index 7d103055a7c..3c83a7b92cd 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -1,8 +1,8 @@ //! Concrete error types for all operations which may be invalid in a certain const context. use hir::{ConstContext, LangItem}; -use rustc_errors::Diag; use rustc_errors::codes::*; +use rustc_errors::{Applicability, Diag}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_infer::infer::TyCtxtInferExt; @@ -388,6 +388,7 @@ pub(crate) struct FnCallUnstable { /// expose on stable. pub feature_enabled: bool, pub safe_to_expose_on_stable: bool, + pub suggestion_span: Option<Span>, } impl<'tcx> NonConstOp<'tcx> for FnCallUnstable { @@ -407,8 +408,18 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable { def_path: ccx.tcx.def_path_str(self.def_id), }); // FIXME: make this translatable + let msg = format!("add `#![feature({})]` to the crate attributes to enable", self.feature); #[allow(rustc::untranslatable_diagnostic)] - err.help(format!("add `#![feature({})]` to the crate attributes to enable", self.feature)); + if let Some(span) = self.suggestion_span { + err.span_suggestion_verbose( + span, + msg, + format!("#![feature({})]\n", self.feature), + Applicability::MachineApplicable, + ); + } else { + err.help(msg); + } err } @@ -436,6 +447,7 @@ pub(crate) struct IntrinsicUnstable { pub name: Symbol, pub feature: Symbol, pub const_stable_indirect: bool, + pub suggestion: Option<Span>, } impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable { @@ -455,6 +467,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable { span, name: self.name, feature: self.feature, + suggestion: self.suggestion, + help: self.suggestion.is_none(), }) } } diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index 3fe78171cd9..1ee9214c4b2 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -123,12 +123,19 @@ pub(crate) struct UnstableConstFn { #[derive(Diagnostic)] #[diag(const_eval_unstable_intrinsic)] -#[help] pub(crate) struct UnstableIntrinsic { #[primary_span] pub span: Span, pub name: Symbol, pub feature: Symbol, + #[suggestion( + const_eval_unstable_intrinsic_suggestion, + code = "#![feature({feature})]\n", + applicability = "machine-applicable" + )] + pub suggestion: Option<Span>, + #[help(const_eval_unstable_intrinsic_suggestion)] + pub help: bool, } #[derive(Diagnostic)] diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index 4eed2bc1238..052adaa69b2 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -19,8 +19,15 @@ use crate::fluent_generated as fluent; pub(crate) struct BaseExpressionDoubleDot { #[primary_span] pub span: Span, + #[suggestion( + hir_typeck_base_expression_double_dot_enable_default_field_values, + code = "#![feature(default_field_values)]\n", + applicability = "machine-applicable", + style = "verbose" + )] + pub default_field_values_suggestion: Option<Span>, #[subdiagnostic] - pub default_field_values: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>, + pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>, #[subdiagnostic] pub add_expr: Option<BaseExpressionDoubleDotAddExpr>, #[subdiagnostic] diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index dc6a2adf622..01fed72d5a2 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2138,13 +2138,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } if !self.tcx.features().default_field_values() { + let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id); self.dcx().emit_err(BaseExpressionDoubleDot { span: span.shrink_to_hi(), // We only mention enabling the feature if this is a nightly rustc *and* the // expression would make sense with the feature enabled. - default_field_values: if self.tcx.sess.is_nightly_build() + default_field_values_suggestion: if self.tcx.sess.is_nightly_build() && missing_mandatory_fields.is_empty() && !missing_optional_fields.is_empty() + && sugg.is_some() + { + sugg + } else { + None + }, + default_field_values_help: if self.tcx.sess.is_nightly_build() + && missing_mandatory_fields.is_empty() + && !missing_optional_fields.is_empty() + && sugg.is_none() { Some(BaseExpressionDoubleDotEnableDefaultFieldValues) } else { |
