Skip to content

Commit 013ac1a

Browse files
Remove spans from StabilityLevel, so it can be decoded in ProcMacroData
1 parent 90a545b commit 013ac1a

File tree

4 files changed

+86
-78
lines changed

4 files changed

+86
-78
lines changed

compiler/rustc_attr/src/builtin.rs

+53-47
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,7 @@ pub struct ConstStability {
114114
#[derive(HashStable_Generic)]
115115
pub enum StabilityLevel {
116116
Unstable { unstables: Vec<Unstability>, is_soft: bool },
117-
Stable { feature: Symbol, since: Symbol, span: Span },
118-
}
119-
120-
impl StabilityLevel {
121-
pub fn spans(&self) -> Vec<Span> {
122-
match self {
123-
StabilityLevel::Stable { span, .. } => vec![*span],
124-
StabilityLevel::Unstable { unstables, .. } => {
125-
unstables.iter().map(|u| u.span).collect()
126-
}
127-
}
128-
}
117+
Stable { feature: Symbol, since: Symbol },
129118
}
130119

131120
/// An instance of the `#[unstable]` attribute
@@ -135,7 +124,6 @@ pub struct Unstability {
135124
pub feature: Symbol,
136125
pub issue: Option<NonZeroU32>,
137126
pub reason: Option<Symbol>,
138-
pub span: Span,
139127
}
140128

141129
impl StabilityLevel {
@@ -162,22 +150,22 @@ pub fn find_stability(
162150
sess: &Session,
163151
attrs: &[Attribute],
164152
item_sp: Span,
165-
) -> (Option<Stability>, Option<ConstStability>) {
153+
) -> (Option<(Stability, Vec<Span>)>, Option<(ConstStability, Vec<Span>)>) {
166154
find_stability_generic(sess, attrs.iter(), item_sp)
167155
}
168156

169157
fn find_stability_generic<'a, I>(
170158
sess: &Session,
171159
attrs_iter: I,
172160
item_sp: Span,
173-
) -> (Option<Stability>, Option<ConstStability>)
161+
) -> (Option<(Stability, Vec<Span>)>, Option<(ConstStability, Vec<Span>)>)
174162
where
175163
I: Iterator<Item = &'a Attribute>,
176164
{
177165
use StabilityLevel::*;
178166

179-
let mut stab: Option<Stability> = None;
180-
let mut const_stab: Option<ConstStability> = None;
167+
let mut stab: Option<(Stability, Vec<Span>)> = None;
168+
let mut const_stab: Option<(ConstStability, Vec<Span>)> = None;
181169
let mut promotable = false;
182170

183171
let diagnostic = &sess.parse_sess.span_diagnostic;
@@ -319,24 +307,33 @@ where
319307
);
320308
continue;
321309
}
322-
let unstability =
323-
Unstability { feature, issue: issue_num, reason, span: attr.span };
310+
let unstability = Unstability { feature, issue: issue_num, reason };
324311
if sym::unstable == meta_name {
325312
match &mut stab {
326313
stab @ None => {
327-
*stab = Some(Stability {
328-
level: StabilityLevel::Unstable {
329-
unstables: vec![unstability],
330-
is_soft,
314+
*stab = Some((
315+
Stability {
316+
level: StabilityLevel::Unstable {
317+
unstables: vec![unstability],
318+
is_soft,
319+
},
331320
},
332-
})
321+
vec![attr.span],
322+
))
333323
}
334324
// Merge multiple unstability attributes into one
335-
Some(Stability {
336-
level:
337-
StabilityLevel::Unstable { unstables, is_soft: old_is_soft },
338-
}) => {
325+
Some((
326+
Stability {
327+
level:
328+
StabilityLevel::Unstable {
329+
unstables,
330+
is_soft: old_is_soft,
331+
},
332+
},
333+
spans,
334+
)) => {
339335
unstables.push(unstability);
336+
spans.push(attr.span);
340337
// FIXME(compiler-errors): Do we want this behavior: is_soft iff all are is_soft?
341338
*old_is_soft &= is_soft;
342339
}
@@ -351,28 +348,34 @@ where
351348
} else {
352349
match &mut const_stab {
353350
const_stab @ None => {
354-
*const_stab = Some(ConstStability {
355-
level: StabilityLevel::Unstable {
356-
unstables: vec![unstability],
357-
is_soft,
351+
*const_stab = Some((
352+
ConstStability {
353+
level: StabilityLevel::Unstable {
354+
unstables: vec![unstability],
355+
is_soft,
356+
},
357+
promotable: false,
358358
},
359-
promotable: false,
360-
})
359+
vec![attr.span],
360+
))
361361
}
362-
Some(ConstStability {
363-
level:
364-
StabilityLevel::Unstable {
365-
unstables: unstability,
366-
is_soft: old_is_soft,
367-
},
368-
..
369-
}) => {
362+
Some((
363+
ConstStability {
364+
level:
365+
StabilityLevel::Unstable {
366+
unstables: unstability,
367+
is_soft: old_is_soft,
368+
},
369+
..
370+
},
371+
spans,
372+
)) => {
370373
unstability.push(Unstability {
371374
feature,
372375
issue: issue_num,
373376
reason,
374-
span: attr.span,
375377
});
378+
spans.push(attr.span);
376379
*old_is_soft &= is_soft;
377380
}
378381
_ => {
@@ -453,11 +456,14 @@ where
453456

454457
match (feature, since) {
455458
(Some(feature), Some(since)) => {
456-
let level = Stable { since, feature, span: attr.span };
459+
let level = Stable { since, feature };
457460
if sym::stable == meta_name {
458-
stab = Some(Stability { level });
461+
stab = Some((Stability { level }, vec![attr.span]));
459462
} else {
460-
const_stab = Some(ConstStability { level, promotable: false });
463+
const_stab = Some((
464+
ConstStability { level, promotable: false },
465+
vec![attr.span],
466+
));
461467
}
462468
}
463469
(None, _) => {
@@ -477,7 +483,7 @@ where
477483

478484
// Merge the const-unstable info into the stability info
479485
if promotable {
480-
if let Some(stab) = &mut const_stab {
486+
if let Some((stab, _)) = &mut const_stab {
481487
stab.promotable = promotable;
482488
} else {
483489
struct_span_err!(

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
927927

928928
// Calling an unstable function *always* requires that the corresponding gate
929929
// be enabled, even if the function has `#[rustc_allow_const_fn_unstable(the_gate)]`.
930-
if !tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == feature)
930+
if !tcx
931+
.features()
932+
.declared_lib_features
933+
.iter()
934+
.any(|&(sym, _)| sym == feature)
931935
{
932936
bad_gates.push(feature);
933937
continue;

compiler/rustc_expand/src/base.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -776,17 +776,16 @@ impl SyntaxExtension {
776776
})
777777
.unwrap_or_else(|| (None, helper_attrs));
778778
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
779-
if let Some(stab) = const_stability {
780-
let spans = stab.level.spans();
781-
let mut diag = sess
782-
.parse_sess
783-
.span_diagnostic
784-
.struct_span_err(spans.clone(), "macros cannot have const stability attributes");
779+
if let Some((_, const_spans)) = const_stability {
780+
let mut diag = sess.parse_sess.span_diagnostic.struct_span_err(
781+
const_spans.clone(),
782+
"macros cannot have const stability attributes",
783+
);
785784
diag.span_label(
786785
sess.source_map().guess_head_span(span),
787786
"const stability attribute affects this macro",
788787
);
789-
for sp in spans {
788+
for sp in const_spans {
790789
diag.span_label(sp, "invalid const stability attribute");
791790
}
792791
diag.emit();
@@ -799,7 +798,7 @@ impl SyntaxExtension {
799798
.then(|| allow_internal_unstable.into()),
800799
allow_internal_unsafe: sess.contains_name(attrs, sym::allow_internal_unsafe),
801800
local_inner_macros,
802-
stability,
801+
stability: stability.map(|(stab, _)| stab),
803802
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
804803
helper_attrs,
805804
edition,

compiler/rustc_passes/src/stability.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -162,39 +162,37 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
162162
}
163163

164164
let (stab, const_stab) = attr::find_stability(&self.tcx.sess, attrs, item_sp);
165-
// Intern stability
166-
let stab = stab.map(|stab| self.tcx.mk_stability(stab));
167-
168-
// Intern const_stability
169-
let const_stab = const_stab.map(|const_stab| self.tcx.mk_const_stability(const_stab));
170165

171166
// If the current node is a function, has const stability attributes and if it doesn not have an intrinsic ABI,
172167
// check if the function/method is const or the parent impl block is const
173-
if let (Some(const_stab), Some(fn_sig)) = (const_stab, fn_sig) {
168+
if let (Some((_, const_spans)), Some(fn_sig)) = (&const_stab, fn_sig) {
174169
if fn_sig.header.abi != Abi::RustIntrinsic
175170
&& fn_sig.header.abi != Abi::PlatformIntrinsic
176171
&& !fn_sig.header.is_const()
177172
{
178173
if !self.in_trait_impl
179174
|| (self.in_trait_impl && !self.tcx.is_const_fn_raw(def_id.to_def_id()))
180175
{
181-
missing_const_err(&self.tcx.sess, fn_sig.span, const_stab.level.spans());
176+
missing_const_err(&self.tcx.sess, fn_sig.span, const_spans);
182177
}
183178
}
184179
}
185180

186181
// `impl const Trait for Type` items forward their const stability to their
187182
// immediate children.
188-
if let Some(const_stab) = const_stab {
183+
let const_stab = if let Some((const_stab, _)) = const_stab {
184+
let const_stab = self.tcx.mk_const_stability(const_stab);
189185
self.index.const_stab_map.insert(def_id, const_stab);
186+
Some(const_stab)
190187
} else {
191188
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
192189
if let Some(parent) = self.parent_const_stab {
193190
if parent.level.is_unstable() {
194191
self.index.const_stab_map.insert(def_id, parent);
195192
}
196193
}
197-
}
194+
None
195+
};
198196

199197
if let Some((rustc_attr::Deprecation { is_since_rustc_version: true, .. }, span)) = &depr {
200198
if stab.is_none() {
@@ -209,17 +207,16 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
209207
}
210208
}
211209

212-
let stab = stab.map(|stab| {
213-
let spans = stab.level.spans();
210+
let stab = stab.map(|(stab, stab_spans)| {
214211
// Error if prohibited, or can't inherit anything from a container.
215212
if kind == AnnotationKind::Prohibited
216213
|| (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
217214
{
218215
let mut diag = self
219216
.tcx
220217
.sess
221-
.struct_span_err(spans.clone(), "this stability annotation is useless");
222-
for sp in &spans {
218+
.struct_span_err(stab_spans.clone(), "this stability annotation is useless");
219+
for sp in &stab_spans {
223220
diag.span_label(*sp, "useless stability annotation");
224221
}
225222
diag.span_label(item_sp, "the stability attribute annotates this item").emit();
@@ -229,9 +226,11 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
229226

230227
// Check if deprecated_since < stable_since. If it is,
231228
// this is *almost surely* an accident.
232-
if let (&Some(dep_since), &attr::Stable { since: stab_since, span, .. }) =
229+
if let (&Some(dep_since), &attr::Stable { since: stab_since, .. }) =
233230
(&depr.as_ref().and_then(|(d, _)| d.since), &stab.level)
234231
{
232+
let span = *stab_spans.first().expect("expected one span with a stable attribute");
233+
235234
// Explicit version of iter::order::lt to handle parse errors properly
236235
for (dep_v, stab_v) in
237236
iter::zip(dep_since.as_str().split('.'), stab_since.as_str().split('.'))
@@ -252,7 +251,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
252251
self.tcx
253252
.sess
254253
.struct_span_err(
255-
spans,
254+
stab_spans,
256255
"an API can't be stabilized after it is deprecated",
257256
)
258257
.span_label(span, "invalid version")
@@ -285,6 +284,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
285284
}
286285
}
287286

287+
let stab = self.tcx.mk_stability(stab);
288288
self.index.stab_map.insert(def_id, stab);
289289
stab
290290
});
@@ -659,7 +659,6 @@ fn stability_index<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
659659
reason: Some(Symbol::intern(reason)),
660660
issue: NonZeroU32::new(27812),
661661
feature: sym::rustc_private,
662-
span: rustc_span::DUMMY_SP,
663662
}],
664663
is_soft: false,
665664
},
@@ -740,18 +739,18 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
740739
// it will have no effect.
741740
// See: https://github.com/rust-lang/rust/issues/55436
742741
let attrs = self.tcx.hir().attrs(item.hir_id());
743-
if let (Some(Stability { level: attr::Unstable { unstables, .. }, .. }), _) =
744-
attr::find_stability(&self.tcx.sess, attrs, item.span)
742+
if let (Some((stab, spans)), _) =
743+
attr::find_stability(&self.tcx.sess, attrs, item.span) && stab.level.is_unstable()
745744
{
746745
let mut c = CheckTraitImplStable { tcx: self.tcx, fully_stable: true };
747746
c.visit_ty(self_ty);
748747
c.visit_trait_ref(t);
749748
if c.fully_stable {
750-
for u in unstables {
749+
for span in spans {
751750
self.tcx.struct_span_lint_hir(
752751
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
753752
item.hir_id(),
754-
u.span,
753+
span,
755754
|lint| {
756755
lint
757756
.build("an `#[unstable]` annotation here has no effect")
@@ -961,14 +960,14 @@ fn duplicate_feature_err(sess: &Session, span: Span, feature: Symbol) {
961960
.emit();
962961
}
963962

964-
fn missing_const_err(session: &Session, fn_sig_span: Span, const_spans: Vec<Span>) {
963+
fn missing_const_err(session: &Session, fn_sig_span: Span, const_spans: &[Span]) {
965964
const ERROR_MSG: &'static str = "attributes `#[rustc_const_unstable]` \
966965
and `#[rustc_const_stable]` require \
967966
the function or method to be `const`";
968967

969968
let mut diag = session.struct_span_err(fn_sig_span, ERROR_MSG);
970969
for sp in const_spans {
971-
diag.span_label(sp, "attribute specified here");
970+
diag.span_label(*sp, "attribute specified here");
972971
}
973972
diag.span_help(fn_sig_span, "make the function or method const").emit();
974973
}

0 commit comments

Comments
 (0)