Skip to content

Commit 19806e4

Browse files
committed
Tweak stability attribute diagnostic output
1 parent 4cb089b commit 19806e4

File tree

5 files changed

+55
-39
lines changed

5 files changed

+55
-39
lines changed

compiler/rustc_attr/src/builtin.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,24 @@ pub fn find_stability(
176176
sess: &Session,
177177
attrs: &[Attribute],
178178
item_sp: Span,
179-
) -> (Option<Stability>, Option<ConstStability>) {
179+
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>) {
180180
find_stability_generic(sess, attrs.iter(), item_sp)
181181
}
182182

183183
fn find_stability_generic<'a, I>(
184184
sess: &Session,
185185
attrs_iter: I,
186186
item_sp: Span,
187-
) -> (Option<Stability>, Option<ConstStability>)
187+
) -> (Option<(Stability, Span)>, Option<(ConstStability, Span)>)
188188
where
189189
I: Iterator<Item = &'a Attribute>,
190190
{
191191
use StabilityLevel::*;
192192

193-
let mut stab: Option<Stability> = None;
194-
let mut const_stab: Option<ConstStability> = None;
193+
let mut stab: Option<(Stability, Span)> = None;
194+
let mut const_stab: Option<(ConstStability, Span)> = None;
195195
let mut promotable = false;
196+
196197
let diagnostic = &sess.parse_sess.span_diagnostic;
197198

198199
'outer: for attr in attrs_iter {
@@ -356,10 +357,12 @@ where
356357
}
357358
let level = Unstable { reason, issue: issue_num, is_soft };
358359
if sym::unstable == meta_name {
359-
stab = Some(Stability { level, feature });
360+
stab = Some((Stability { level, feature }, attr.span));
360361
} else {
361-
const_stab =
362-
Some(ConstStability { level, feature, promotable: false });
362+
const_stab = Some((
363+
ConstStability { level, feature, promotable: false },
364+
attr.span,
365+
));
363366
}
364367
}
365368
(None, _, _) => {
@@ -432,10 +435,12 @@ where
432435
(Some(feature), Some(since)) => {
433436
let level = Stable { since };
434437
if sym::stable == meta_name {
435-
stab = Some(Stability { level, feature });
438+
stab = Some((Stability { level, feature }, attr.span));
436439
} else {
437-
const_stab =
438-
Some(ConstStability { level, feature, promotable: false });
440+
const_stab = Some((
441+
ConstStability { level, feature, promotable: false },
442+
attr.span,
443+
));
439444
}
440445
}
441446
(None, _) => {
@@ -455,7 +460,7 @@ where
455460

456461
// Merge the const-unstable info into the stability info
457462
if promotable {
458-
if let Some(ref mut stab) = const_stab {
463+
if let Some((ref mut stab, _)) = const_stab {
459464
stab.promotable = promotable;
460465
} else {
461466
struct_span_err!(

compiler/rustc_expand/src/base.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,13 @@ impl SyntaxExtension {
770770
.find_by_name(attrs, sym::rustc_builtin_macro)
771771
.map(|a| a.value_str().unwrap_or(name));
772772
let (stability, const_stability) = attr::find_stability(&sess, attrs, span);
773-
if const_stability.is_some() {
773+
if let Some((_, sp)) = const_stability {
774774
sess.parse_sess
775775
.span_diagnostic
776-
.span_err(span, "macros cannot have const stability attributes");
776+
.struct_span_err(sp, "macros cannot have const stability attributes")
777+
.span_label(sp, "invalid stability attribute")
778+
.span_label(span, "in this macro")
779+
.emit();
777780
}
778781

779782
SyntaxExtension {
@@ -782,7 +785,7 @@ impl SyntaxExtension {
782785
allow_internal_unstable,
783786
allow_internal_unsafe: sess.contains_name(attrs, sym::allow_internal_unsafe),
784787
local_inner_macros,
785-
stability,
788+
stability: stability.map(|(s, _)| s),
786789
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
787790
helper_attrs,
788791
edition,

compiler/rustc_passes/src/stability.rs

+19-17
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
163163

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

166-
let const_stab = const_stab.map(|const_stab| {
166+
let const_stab = const_stab.map(|(const_stab, _)| {
167167
let const_stab = self.tcx.intern_const_stability(const_stab);
168168
self.index.const_stab_map.insert(hir_id, const_stab);
169169
const_stab
@@ -193,12 +193,15 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
193193
}
194194
}
195195

196-
let stab = stab.map(|stab| {
196+
let stab = stab.map(|(stab, span)| {
197197
// Error if prohibited, or can't inherit anything from a container.
198198
if kind == AnnotationKind::Prohibited
199199
|| (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
200200
{
201-
self.tcx.sess.span_err(item_sp, "this stability annotation is useless");
201+
self.tcx.sess.struct_span_err(span,"this stability annotation is useless")
202+
.span_label(span, "useless stability annotation")
203+
.span_label(item_sp, "the stability attribute annotates this item")
204+
.emit();
202205
}
203206

204207
debug!("annotate: found {:?}", stab);
@@ -215,26 +218,30 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
215218
{
216219
match stab_v.parse::<u64>() {
217220
Err(_) => {
218-
self.tcx.sess.span_err(item_sp, "invalid stability version found");
221+
self.tcx.sess.struct_span_err(span, "invalid stability version found")
222+
.span_label(span, "invalid stability version")
223+
.span_label(item_sp, "the stability attribute annotates this item")
224+
.emit();
219225
break;
220226
}
221227
Ok(stab_vp) => match dep_v.parse::<u64>() {
222228
Ok(dep_vp) => match dep_vp.cmp(&stab_vp) {
223229
Ordering::Less => {
224-
self.tcx.sess.span_err(
225-
item_sp,
226-
"an API can't be stabilized after it is deprecated",
227-
);
230+
self.tcx.sess.struct_span_err(span, "an API can't be stabilized after it is deprecated")
231+
.span_label(span, "invalid version")
232+
.span_label(item_sp, "the stability attribute annotates this item")
233+
.emit();
228234
break;
229235
}
230236
Ordering::Equal => continue,
231237
Ordering::Greater => break,
232238
},
233239
Err(_) => {
234240
if dep_v != "TBD" {
235-
self.tcx
236-
.sess
237-
.span_err(item_sp, "invalid deprecation version found");
241+
self.tcx.sess.struct_span_err(span, "invalid deprecation version found")
242+
.span_label(span, "invalid deprecation version")
243+
.span_label(item_sp, "the stability attribute annotates this item")
244+
.emit();
238245
}
239246
break;
240247
}
@@ -756,18 +763,13 @@ impl Visitor<'tcx> for Checker<'tcx> {
756763
// error if all involved types and traits are stable, because
757764
// it will have no effect.
758765
// See: https://github.com/rust-lang/rust/issues/55436
759-
if let (Some(Stability { level: attr::Unstable { .. }, .. }), _) =
766+
if let (Some((Stability { level: attr::Unstable { .. }, .. }, span)), _) =
760767
attr::find_stability(&self.tcx.sess, &item.attrs, item.span)
761768
{
762769
let mut c = CheckTraitImplStable { tcx: self.tcx, fully_stable: true };
763770
c.visit_ty(self_ty);
764771
c.visit_trait_ref(t);
765772
if c.fully_stable {
766-
let span = item
767-
.attrs
768-
.iter()
769-
.find(|a| a.has_name(sym::unstable))
770-
.map_or(item.span, |a| a.span);
771773
self.tcx.struct_span_lint_hir(
772774
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
773775
item.hir_id,

src/test/ui/stability-attribute/stability-attribute-sanity.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ fn multiple2() { }
5757
#[stable(feature = "a", since = "b")] //~ ERROR multiple stability levels [E0544]
5858
fn multiple3() { }
5959

60-
#[stable(feature = "a", since = "b")]
60+
#[stable(feature = "a", since = "b")] //~ ERROR invalid stability version found
6161
#[rustc_deprecated(since = "b", reason = "text")]
6262
#[rustc_deprecated(since = "b", reason = "text")] //~ ERROR multiple deprecated attributes
6363
#[rustc_const_unstable(feature = "c", issue = "none")]
6464
#[rustc_const_unstable(feature = "d", issue = "none")] //~ ERROR multiple stability levels
65-
pub const fn multiple4() { } //~ ERROR invalid stability version found
65+
pub const fn multiple4() { }
6666

67-
#[stable(feature = "a", since = "1.0.0")]
67+
#[stable(feature = "a", since = "1.0.0")] //~ ERROR invalid deprecation version found
6868
#[rustc_deprecated(since = "invalid", reason = "text")]
69-
fn invalid_deprecation_version() {} //~ ERROR invalid deprecation version found
69+
fn invalid_deprecation_version() {}
7070

7171
#[rustc_deprecated(since = "a", reason = "text")]
7272
fn deprecated_without_unstable_or_stable() { }

src/test/ui/stability-attribute/stability-attribute-sanity.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,22 @@ LL | #[rustc_const_unstable(feature = "d", issue = "none")]
9797
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9898

9999
error: invalid stability version found
100-
--> $DIR/stability-attribute-sanity.rs:65:1
100+
--> $DIR/stability-attribute-sanity.rs:60:1
101101
|
102+
LL | #[stable(feature = "a", since = "b")]
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid stability version
104+
...
102105
LL | pub const fn multiple4() { }
103-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106+
| ---------------------------- the stability attribute annotates this item
104107

105108
error: invalid deprecation version found
106-
--> $DIR/stability-attribute-sanity.rs:69:1
109+
--> $DIR/stability-attribute-sanity.rs:67:1
107110
|
111+
LL | #[stable(feature = "a", since = "1.0.0")]
112+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid deprecation version
113+
LL | #[rustc_deprecated(since = "invalid", reason = "text")]
108114
LL | fn invalid_deprecation_version() {}
109-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
| ----------------------------------- the stability attribute annotates this item
110116

111117
error[E0549]: rustc_deprecated attribute must be paired with either stable or unstable attribute
112118
--> $DIR/stability-attribute-sanity.rs:71:1

0 commit comments

Comments
 (0)