Skip to content

Commit 02f7317

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 02f7317

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

compiler/rustc_const_eval/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ const_eval_unstable_in_stable_exposed =
422422
423423
const_eval_unstable_intrinsic = `{$name}` is not yet stable as a const intrinsic
424424
.help = add `#![feature({$feature})]` to the crate attributes to enable
425+
const_eval_unstable_intrinsic_suggestion = add `#![feature({$feature})]` to the crate attributes to enable
425426
426427
const_eval_unterminated_c_string =
427428
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
@@ -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

+3
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

+8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ pub(crate) struct UnstableIntrinsic {
129129
pub span: Span,
130130
pub name: Symbol,
131131
pub feature: Symbol,
132+
#[suggestion(
133+
const_eval_unstable_intrinsic_suggestion,
134+
code = "#![feature(\"{feature}\")]\n",
135+
applicability = "machine-applicable"
136+
)]
137+
pub suggestion: Option<Span>,
138+
#[help]
139+
pub help: bool,
132140
}
133141

134142
#[derive(Diagnostic)]

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

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ LL | unstable_intrinsic::size_of_val(&x);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626
|
2727
= help: add `#![feature(unstable)]` to the crate attributes to enable
28+
help: add `#![feature(unstable)]` to the crate attributes to enable
29+
|
30+
LL + #![feature("unstable")]
31+
|
2832

2933
error: `min_align_of_val` is not yet stable as a const intrinsic
3034
--> $DIR/const-unstable-intrinsic.rs:20:9
@@ -33,6 +37,10 @@ LL | unstable_intrinsic::min_align_of_val(&x);
3337
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3438
|
3539
= help: add `#![feature(unstable)]` to the crate attributes to enable
40+
help: add `#![feature(unstable)]` to the crate attributes to enable
41+
|
42+
LL + #![feature("unstable")]
43+
|
3644

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

0 commit comments

Comments
 (0)