Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 00ed975

Browse files
author
Lamb
committedJul 3, 2021
fn marked with const stability must be const
1 parent 80319d4 commit 00ed975

File tree

4 files changed

+107
-18
lines changed

4 files changed

+107
-18
lines changed
 

‎compiler/rustc_passes/src/stability.rs

+43-18
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ struct Annotator<'a, 'tcx> {
9797
impl<'a, 'tcx> Annotator<'a, 'tcx> {
9898
// Determine the stability for a node based on its attributes and inherited
9999
// stability. The stability is recorded in the index and used as the parent.
100+
// fn_sig: Function signature linked with the current node
100101
fn annotate<F>(
101102
&mut self,
102103
hir_id: HirId,
103104
item_sp: Span,
104-
item_kind: Option<&ItemKind<'_>>,
105+
fn_sig: Option<&hir::FnSig<'_>>,
105106
kind: AnnotationKind,
106107
inherit_deprecation: InheritDeprecation,
107108
inherit_const_stability: InheritConstStability,
@@ -169,20 +170,28 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
169170

170171
let const_stab = const_stab.map(|(const_stab, const_span)| {
171172
let const_stab = self.tcx.intern_const_stability(const_stab);
172-
match item_kind {
173-
Some(ItemKind::Fn(ref fn_sig, _, _)) => {
174-
if !(fn_sig.header.constness == Constness::Const
175-
&& fn_sig.header.abi != Abi::RustIntrinsic
176-
&& fn_sig.header.abi != Abi::PlatformIntrinsic)
177-
{
178-
self.tcx
179-
.sess
180-
.struct_span_err(fn_sig.span, "Attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function to be marked `const`")
181-
.span_label(const_span, "Const stability attribute specified here")
182-
.emit();
183-
}
173+
if let Some(fn_sig) = fn_sig {
174+
if !(fn_sig.header.constness == hir::Constness::Const
175+
&& fn_sig.header.abi != Abi::RustIntrinsic
176+
&& fn_sig.header.abi != Abi::PlatformIntrinsic)
177+
{
178+
self.tcx
179+
.sess
180+
.struct_span_err(
181+
fn_sig.span,
182+
"attributes `#[rustc_const_unstable]` \
183+
and `#[rustc_const_stable]` require \
184+
the function or method to be marked `const`",
185+
)
186+
.span_suggestion_short(
187+
fn_sig.span,
188+
"make the function or method const",
189+
String::new(),
190+
rustc_errors::Applicability::Unspecified,
191+
)
192+
.span_label(const_span, "attribute specified here")
193+
.emit();
184194
}
185-
_ => {}
186195
}
187196

188197
self.index.const_stab_map.insert(hir_id, const_stab);
@@ -386,6 +395,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
386395
let orig_in_trait_impl = self.in_trait_impl;
387396
let mut kind = AnnotationKind::Required;
388397
let mut const_stab_inherit = InheritConstStability::No;
398+
let mut fn_sig = None;
399+
389400
match i.kind {
390401
// Inherent impls and foreign modules serve only as containers for other items,
391402
// they don't have their own stability. They still can be annotated as unstable
@@ -406,7 +417,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
406417
self.annotate(
407418
ctor_hir_id,
408419
i.span,
409-
Some(&i.kind),
420+
None,
410421
AnnotationKind::Required,
411422
InheritDeprecation::Yes,
412423
InheritConstStability::No,
@@ -415,13 +426,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
415426
)
416427
}
417428
}
429+
hir::ItemKind::Fn(ref item_fn_sig, _, _) => {
430+
fn_sig = Some(item_fn_sig);
431+
}
418432
_ => {}
419433
}
420434

421435
self.annotate(
422436
i.hir_id(),
423437
i.span,
424-
Some(&i.kind),
438+
fn_sig,
425439
kind,
426440
InheritDeprecation::Yes,
427441
const_stab_inherit,
@@ -432,10 +446,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
432446
}
433447

434448
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
449+
let fn_sig = match ti.kind {
450+
hir::TraitItemKind::Fn(ref fn_sig, _) => Some(fn_sig),
451+
_ => None,
452+
};
453+
435454
self.annotate(
436455
ti.hir_id(),
437456
ti.span,
438-
None,
457+
fn_sig,
439458
AnnotationKind::Required,
440459
InheritDeprecation::Yes,
441460
InheritConstStability::No,
@@ -449,10 +468,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
449468
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
450469
let kind =
451470
if self.in_trait_impl { AnnotationKind::Prohibited } else { AnnotationKind::Required };
471+
472+
let fn_sig = match ii.kind {
473+
hir::ImplItemKind::Fn(ref fn_sig, _) => Some(fn_sig),
474+
_ => None,
475+
};
476+
452477
self.annotate(
453478
ii.hir_id(),
454479
ii.span,
455-
None,
480+
fn_sig,
456481
kind,
457482
InheritDeprecation::Yes,
458483
InheritConstStability::No,

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,4 @@ mod core_arch;
308308

309309
#[stable(feature = "simd_arch", since = "1.27.0")]
310310
pub use core_arch::arch;
311+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![crate_type = "lib"]
2+
#![feature(staged_api)]
3+
#![stable(feature = "foo", since = "1.0.0")]
4+
5+
#[stable(feature = "foo", since = "1.0.0")]
6+
#[rustc_const_unstable(feature = "const_foo", issue = "none")]
7+
pub fn foo() {}
8+
//~^ ERROR attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
9+
10+
#[stable(feature = "bar", since = "1.0.0")]
11+
#[rustc_const_stable(feature = "const_bar", since = "1.0.0")]
12+
pub fn bar() {}
13+
//~^ ERROR attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
14+
15+
#[stable(feature = "potato", since = "1.0.0")]
16+
pub struct Potato;
17+
18+
impl Potato {
19+
#[stable(feature = "salad", since = "1.0.0")]
20+
#[rustc_const_unstable(feature = "const_salad", issue = "none")]
21+
pub fn salad(&self) -> &'static str { "mmmmmm" }
22+
//~^ ERROR attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
23+
24+
#[stable(feature = "roasted", since = "1.0.0")]
25+
#[rustc_const_unstable(feature = "const_roasted", issue = "none")]
26+
pub fn roasted(&self) -> &'static str { "mmmmmmmmmm" }
27+
//~^ ERROR attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
28+
}
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
2+
--> $DIR/rustc-const-stability-require-const.rs:7:1
3+
|
4+
LL | #[rustc_const_unstable(feature = "const_foo", issue = "none")]
5+
| -------------------------------------------------------------- attribute specified here
6+
LL | pub fn foo() {}
7+
| ^^^^^^^^^^^^ help: make the function or method const
8+
9+
error: attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
10+
--> $DIR/rustc-const-stability-require-const.rs:12:1
11+
|
12+
LL | #[rustc_const_stable(feature = "const_bar", since = "1.0.0")]
13+
| ------------------------------------------------------------- attribute specified here
14+
LL | pub fn bar() {}
15+
| ^^^^^^^^^^^^ help: make the function or method const
16+
17+
error: attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
18+
--> $DIR/rustc-const-stability-require-const.rs:21:5
19+
|
20+
LL | #[rustc_const_unstable(feature = "const_salad", issue = "none")]
21+
| ---------------------------------------------------------------- attribute specified here
22+
LL | pub fn salad(&self) -> &'static str { "mmmmmm" }
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: make the function or method const
24+
25+
error: attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function or method to be marked `const`
26+
--> $DIR/rustc-const-stability-require-const.rs:26:5
27+
|
28+
LL | #[rustc_const_unstable(feature = "const_roasted", issue = "none")]
29+
| ------------------------------------------------------------------ attribute specified here
30+
LL | pub fn roasted(&self) -> &'static str { "mmmmmmmmmm" }
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: make the function or method const
32+
33+
error: aborting due to 4 previous errors
34+

0 commit comments

Comments
 (0)
Please sign in to comment.