Skip to content

Commit ab6feeb

Browse files
authored
Unrolled build for rust-lang#134858
Rollup merge of rust-lang#134858 - estebank:issue-81370, r=Noratrieb Provide structured suggestion for `#![feature(..)]` in more cases Fix rust-lang#81370.
2 parents 678e669 + e68a8ce commit ab6feeb

File tree

9 files changed

+106
-20
lines changed

9 files changed

+106
-20
lines changed

compiler/rustc_const_eval/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ const_eval_unstable_in_stable_exposed =
424424
.bypass_sugg = otherwise, as a last resort `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (this requires team approval)
425425
426426
const_eval_unstable_intrinsic = `{$name}` is not yet stable as a const intrinsic
427-
.help = add `#![feature({$feature})]` to the crate attributes to enable
427+
const_eval_unstable_intrinsic_suggestion = add `#![feature({$feature})]` to the crate attributes to enable
428428
429429
const_eval_unterminated_c_string =
430430
reading a null-terminated string starting at {$pointer} with no null found before end of allocation

compiler/rustc_const_eval/src/check_consts/check.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
464464
err_span,
465465
);
466466
}
467+
468+
fn crate_inject_span(&self) -> Option<Span> {
469+
self.tcx.hir_crate_items(()).definitions().next().and_then(|id| {
470+
self.tcx.crate_level_attribute_injection_span(self.tcx.local_def_id_to_hir_id(id))
471+
})
472+
}
467473
}
468474

469475
impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
@@ -809,6 +815,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
809815
name: intrinsic.name,
810816
feature,
811817
const_stable_indirect: is_const_stable,
818+
suggestion: self.crate_inject_span(),
812819
});
813820
}
814821
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => {
@@ -897,7 +904,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
897904
// regular stability.
898905
feature == sym::rustc_private
899906
&& issue == NonZero::new(27812)
900-
&& self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
907+
&& tcx.sess.opts.unstable_opts.force_unstable_if_unmarked
901908
};
902909
// Even if the feature is enabled, we still need check_op to double-check
903910
// this if the callee is not safe to expose on stable.
@@ -907,6 +914,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
907914
feature,
908915
feature_enabled,
909916
safe_to_expose_on_stable: callee_safe_to_expose_on_stable,
917+
suggestion_span: self.crate_inject_span(),
910918
});
911919
}
912920
}

compiler/rustc_const_eval/src/check_consts/ops.rs

+16-2
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;
54
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;
@@ -388,6 +388,7 @@ pub(crate) struct FnCallUnstable {
388388
/// expose on stable.
389389
pub feature_enabled: bool,
390390
pub safe_to_expose_on_stable: bool,
391+
pub suggestion_span: Option<Span>,
391392
}
392393

393394
impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
@@ -407,8 +408,18 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
407408
def_path: ccx.tcx.def_path_str(self.def_id),
408409
});
409410
// FIXME: make this translatable
411+
let msg = format!("add `#![feature({})]` to the crate attributes to enable", self.feature);
410412
#[allow(rustc::untranslatable_diagnostic)]
411-
err.help(format!("add `#![feature({})]` to the crate attributes to enable", self.feature));
413+
if let Some(span) = self.suggestion_span {
414+
err.span_suggestion_verbose(
415+
span,
416+
msg,
417+
format!("#![feature({})]\n", self.feature),
418+
Applicability::MachineApplicable,
419+
);
420+
} else {
421+
err.help(msg);
422+
}
412423

413424
err
414425
}
@@ -436,6 +447,7 @@ pub(crate) struct IntrinsicUnstable {
436447
pub name: Symbol,
437448
pub feature: Symbol,
438449
pub const_stable_indirect: bool,
450+
pub suggestion: Option<Span>,
439451
}
440452

441453
impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
@@ -455,6 +467,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
455467
span,
456468
name: self.name,
457469
feature: self.feature,
470+
suggestion: self.suggestion,
471+
help: self.suggestion.is_none(),
458472
})
459473
}
460474
}

compiler/rustc_const_eval/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,19 @@ pub(crate) struct UnstableConstFn {
123123

124124
#[derive(Diagnostic)]
125125
#[diag(const_eval_unstable_intrinsic)]
126-
#[help]
127126
pub(crate) struct UnstableIntrinsic {
128127
#[primary_span]
129128
pub span: Span,
130129
pub name: Symbol,
131130
pub feature: Symbol,
131+
#[suggestion(
132+
const_eval_unstable_intrinsic_suggestion,
133+
code = "#![feature({feature})]\n",
134+
applicability = "machine-applicable"
135+
)]
136+
pub suggestion: Option<Span>,
137+
#[help(const_eval_unstable_intrinsic_suggestion)]
138+
pub help: bool,
132139
}
133140

134141
#[derive(Diagnostic)]

compiler/rustc_hir_typeck/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ use crate::fluent_generated as fluent;
1919
pub(crate) struct BaseExpressionDoubleDot {
2020
#[primary_span]
2121
pub span: Span,
22+
#[suggestion(
23+
hir_typeck_base_expression_double_dot_enable_default_field_values,
24+
code = "#![feature(default_field_values)]\n",
25+
applicability = "machine-applicable",
26+
style = "verbose"
27+
)]
28+
pub default_field_values_suggestion: Option<Span>,
2229
#[subdiagnostic]
23-
pub default_field_values: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
30+
pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
2431
#[subdiagnostic]
2532
pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
2633
#[subdiagnostic]

compiler/rustc_hir_typeck/src/expr.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2138,13 +2138,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21382138
}
21392139
}
21402140
if !self.tcx.features().default_field_values() {
2141+
let sugg = self.tcx.crate_level_attribute_injection_span(expr.hir_id);
21412142
self.dcx().emit_err(BaseExpressionDoubleDot {
21422143
span: span.shrink_to_hi(),
21432144
// We only mention enabling the feature if this is a nightly rustc *and* the
21442145
// expression would make sense with the feature enabled.
2145-
default_field_values: if self.tcx.sess.is_nightly_build()
2146+
default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
21462147
&& missing_mandatory_fields.is_empty()
21472148
&& !missing_optional_fields.is_empty()
2149+
&& sugg.is_some()
2150+
{
2151+
sugg
2152+
} else {
2153+
None
2154+
},
2155+
default_field_values_help: if self.tcx.sess.is_nightly_build()
2156+
&& missing_mandatory_fields.is_empty()
2157+
&& !missing_optional_fields.is_empty()
2158+
&& sugg.is_none()
21482159
{
21492160
Some(BaseExpressionDoubleDotEnableDefaultFieldValues)
21502161
} else {

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

+8-2
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: `min_align_of_val` is not yet stable as a const intrinsic
3033
--> $DIR/const-unstable-intrinsic.rs:20:9
3134
|
3235
LL | unstable_intrinsic::min_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/feature-gates/feature-gate-default-field-values.stderr

+40-10
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ error[E0797]: base expression required after `..`
130130
LL | let x = Foo { .. };
131131
| ^
132132
|
133-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
133+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
134+
|
135+
LL + #![feature(default_field_values)]
136+
|
134137
help: add a base expression here
135138
|
136139
LL | let x = Foo { ../* expr */ };
@@ -142,7 +145,10 @@ error[E0797]: base expression required after `..`
142145
LL | let z = Foo { baz: 1, .. };
143146
| ^
144147
|
145-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
148+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
149+
|
150+
LL + #![feature(default_field_values)]
151+
|
146152
help: add a base expression here
147153
|
148154
LL | let z = Foo { baz: 1, ../* expr */ };
@@ -154,7 +160,10 @@ error[E0797]: base expression required after `..`
154160
LL | let x = Bar::Foo { .. };
155161
| ^
156162
|
157-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
163+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
164+
|
165+
LL + #![feature(default_field_values)]
166+
|
158167
help: add a base expression here
159168
|
160169
LL | let x = Bar::Foo { ../* expr */ };
@@ -166,7 +175,10 @@ error[E0797]: base expression required after `..`
166175
LL | let z = Bar::Foo { baz: 1, .. };
167176
| ^
168177
|
169-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
178+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
179+
|
180+
LL + #![feature(default_field_values)]
181+
|
170182
help: add a base expression here
171183
|
172184
LL | let z = Bar::Foo { baz: 1, ../* expr */ };
@@ -178,7 +190,10 @@ error[E0797]: base expression required after `..`
178190
LL | let x = Qux::<i32, 4> { .. };
179191
| ^
180192
|
181-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
193+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
194+
|
195+
LL + #![feature(default_field_values)]
196+
|
182197
help: add a base expression here
183198
|
184199
LL | let x = Qux::<i32, 4> { ../* expr */ };
@@ -190,7 +205,10 @@ error[E0797]: base expression required after `..`
190205
LL | assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, bay: 4, .. }, x));
191206
| ^
192207
|
193-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
208+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
209+
|
210+
LL + #![feature(default_field_values)]
211+
|
194212
help: add a base expression here
195213
|
196214
LL | assert!(matches!(Qux::<i32, 4> { bar: S, baz: 42, bat: 2, bay: 4, ../* expr */ }, x));
@@ -202,7 +220,10 @@ error[E0797]: base expression required after `..`
202220
LL | let y = Opt { mandatory: None, .. };
203221
| ^
204222
|
205-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
223+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
224+
|
225+
LL + #![feature(default_field_values)]
226+
|
206227
help: add a base expression here
207228
|
208229
LL | let y = Opt { mandatory: None, ../* expr */ };
@@ -214,7 +235,10 @@ error[E0797]: base expression required after `..`
214235
LL | assert!(matches!(Opt { mandatory: None, .. }, z));
215236
| ^
216237
|
217-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
238+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
239+
|
240+
LL + #![feature(default_field_values)]
241+
|
218242
help: add a base expression here
219243
|
220244
LL | assert!(matches!(Opt { mandatory: None, ../* expr */ }, z));
@@ -260,7 +284,10 @@ error[E0797]: base expression required after `..`
260284
LL | let y = OptEnum::Variant { mandatory: None, .. };
261285
| ^
262286
|
263-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
287+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
288+
|
289+
LL + #![feature(default_field_values)]
290+
|
264291
help: add a base expression here
265292
|
266293
LL | let y = OptEnum::Variant { mandatory: None, ../* expr */ };
@@ -272,7 +299,10 @@ error[E0797]: base expression required after `..`
272299
LL | assert!(matches!(OptEnum::Variant { mandatory: None, .. }, z));
273300
| ^
274301
|
275-
= help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
302+
help: add `#![feature(default_field_values)]` to the crate attributes to enable default values on `struct` fields
303+
|
304+
LL + #![feature(default_field_values)]
305+
|
276306
help: add a base expression here
277307
|
278308
LL | assert!(matches!(OptEnum::Variant { mandatory: None, ../* expr */ }, z));

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

+4-1
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

0 commit comments

Comments
 (0)