Skip to content

Commit 4570255

Browse files
author
Lamb
committed
checking if function is const if marked
1 parent 2545459 commit 4570255

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

compiler/rustc_passes/src/stability.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A pass that annotates every item and method with its stability level,
22
//! propagating default levels lexically from parent to children ast nodes.
33
4+
use hir::{Constness, ItemKind};
45
use rustc_ast::Attribute;
56
use rustc_attr::{self as attr, ConstStability, Stability};
67
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -20,6 +21,7 @@ use rustc_session::parse::feature_err;
2021
use rustc_session::Session;
2122
use rustc_span::symbol::{sym, Symbol};
2223
use rustc_span::{Span, DUMMY_SP};
24+
use rustc_target::spec::abi::Abi;
2325

2426
use std::cmp::Ordering;
2527
use std::iter;
@@ -99,6 +101,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
99101
&mut self,
100102
hir_id: HirId,
101103
item_sp: Span,
104+
item_kind: Option<&ItemKind<'_>>,
102105
kind: AnnotationKind,
103106
inherit_deprecation: InheritDeprecation,
104107
inherit_const_stability: InheritConstStability,
@@ -164,8 +167,24 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
164167

165168
let (stab, const_stab) = attr::find_stability(&self.tcx.sess, attrs, item_sp);
166169

167-
let const_stab = const_stab.map(|(const_stab, _)| {
170+
let const_stab = const_stab.map(|(const_stab, const_span)| {
168171
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+
}
184+
}
185+
_ => {}
186+
}
187+
169188
self.index.const_stab_map.insert(hir_id, const_stab);
170189
const_stab
171190
});
@@ -186,7 +205,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
186205
struct_span_err!(
187206
self.tcx.sess,
188207
*span,
189-
E0549,
208+
e0549,
190209
"rustc_deprecated attribute must be paired with \
191210
either stable or unstable attribute"
192211
)
@@ -387,6 +406,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
387406
self.annotate(
388407
ctor_hir_id,
389408
i.span,
409+
Some(&i.kind),
390410
AnnotationKind::Required,
391411
InheritDeprecation::Yes,
392412
InheritConstStability::No,
@@ -401,6 +421,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
401421
self.annotate(
402422
i.hir_id(),
403423
i.span,
424+
Some(&i.kind),
404425
kind,
405426
InheritDeprecation::Yes,
406427
const_stab_inherit,
@@ -414,6 +435,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
414435
self.annotate(
415436
ti.hir_id(),
416437
ti.span,
438+
None,
417439
AnnotationKind::Required,
418440
InheritDeprecation::Yes,
419441
InheritConstStability::No,
@@ -430,6 +452,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
430452
self.annotate(
431453
ii.hir_id(),
432454
ii.span,
455+
None,
433456
kind,
434457
InheritDeprecation::Yes,
435458
InheritConstStability::No,
@@ -444,6 +467,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
444467
self.annotate(
445468
var.id,
446469
var.span,
470+
None,
447471
AnnotationKind::Required,
448472
InheritDeprecation::Yes,
449473
InheritConstStability::No,
@@ -453,6 +477,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
453477
v.annotate(
454478
ctor_hir_id,
455479
var.span,
480+
None,
456481
AnnotationKind::Required,
457482
InheritDeprecation::Yes,
458483
InheritConstStability::No,
@@ -470,6 +495,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
470495
self.annotate(
471496
s.hir_id,
472497
s.span,
498+
None,
473499
AnnotationKind::Required,
474500
InheritDeprecation::Yes,
475501
InheritConstStability::No,
@@ -484,6 +510,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
484510
self.annotate(
485511
i.hir_id(),
486512
i.span,
513+
None,
487514
AnnotationKind::Required,
488515
InheritDeprecation::Yes,
489516
InheritConstStability::No,
@@ -498,6 +525,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
498525
self.annotate(
499526
md.hir_id(),
500527
md.span,
528+
None,
501529
AnnotationKind::Required,
502530
InheritDeprecation::Yes,
503531
InheritConstStability::No,
@@ -517,6 +545,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
517545
self.annotate(
518546
p.hir_id,
519547
p.span,
548+
None,
520549
kind,
521550
InheritDeprecation::No,
522551
InheritConstStability::No,
@@ -687,6 +716,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
687716
annotator.annotate(
688717
hir::CRATE_HIR_ID,
689718
krate.item.inner,
719+
None,
690720
AnnotationKind::Required,
691721
InheritDeprecation::Yes,
692722
InheritConstStability::No,

0 commit comments

Comments
 (0)