Skip to content

Commit 7175da5

Browse files
committed
Structured suggestion for "missing feature intrinsic"
``` error: `size_of_val` is not yet stable as a const intrinsic --> $DIR/const-unstable-intrinsic.rs:17:9 | LL | unstable_intrinsic::size_of_val(&x); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: add `#![feature(unstable)]` to the crate attributes to enable help: add `#![feature(unstable)]` to the crate attributes to enable | LL + #![feature("unstable")] | ```
1 parent a47fee5 commit 7175da5

File tree

5 files changed

+27
-4
lines changed

5 files changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,17 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
819819
feature,
820820
..
821821
}) => {
822+
let suggestion =
823+
tcx.hir_crate_items(()).definitions().next().and_then(|id| {
824+
tcx.crate_level_attribute_injection_span(
825+
tcx.local_def_id_to_hir_id(id),
826+
)
827+
});
822828
self.check_op(ops::IntrinsicUnstable {
823829
name: intrinsic.name,
824830
feature,
825831
const_stable_indirect: is_const_stable,
832+
suggestion,
826833
});
827834
}
828835
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => {

compiler/rustc_const_eval/src/check_consts/ops.rs

+3
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ pub(crate) struct IntrinsicUnstable {
447447
pub name: Symbol,
448448
pub feature: Symbol,
449449
pub const_stable_indirect: bool,
450+
pub suggestion: Option<Span>,
450451
}
451452

452453
impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
@@ -466,6 +467,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
466467
span,
467468
name: self.name,
468469
feature: self.feature,
470+
suggestion: self.suggestion,
471+
help: self.suggestion.is_none(),
469472
})
470473
}
471474
}

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)]

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

0 commit comments

Comments
 (0)