Skip to content

Commit 5770c7e

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 ed56c58 commit 5770c7e

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ const_eval_unstable_in_stable_exposed =
421421
.bypass_sugg = otherwise, as a last resort `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (this requires team approval)
422422
423423
const_eval_unstable_intrinsic = `{$name}` is not yet stable as a const intrinsic
424-
.help = add `#![feature({$feature})]` to the crate attributes to enable
424+
const_eval_unstable_intrinsic_suggestion = add `#![feature({$feature})]` to the crate attributes to enable
425425
426426
const_eval_unterminated_c_string =
427427
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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,17 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
799799
feature,
800800
..
801801
}) => {
802+
let suggestion =
803+
tcx.hir_crate_items(()).definitions().next().and_then(|id| {
804+
tcx.crate_level_attribute_injection_span(
805+
tcx.local_def_id_to_hir_id(id),
806+
)
807+
});
802808
self.check_op(ops::IntrinsicUnstable {
803809
name: intrinsic.name,
804810
feature,
805811
const_stable_indirect: is_const_stable,
812+
suggestion,
806813
});
807814
}
808815
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => {

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ pub(crate) struct IntrinsicUnstable {
394394
pub name: Symbol,
395395
pub feature: Symbol,
396396
pub const_stable_indirect: bool,
397+
pub suggestion: Option<Span>,
397398
}
398399

399400
impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
@@ -413,6 +414,8 @@ impl<'tcx> NonConstOp<'tcx> for IntrinsicUnstable {
413414
span,
414415
name: self.name,
415416
feature: self.feature,
417+
suggestion: self.suggestion,
418+
help: self.suggestion.is_none(),
416419
})
417420
}
418421
}

compiler/rustc_const_eval/src/errors.rs

Lines changed: 8 additions & 1 deletion
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

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: `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)