Skip to content

Commit 55c5127

Browse files
Rollup merge of rust-lang#79509 - GuillaumeGomez:improve-attr-spans, r=oli-obk
Improve attribute message error spans I got the idea while working on rust-lang#79464
2 parents 0206dd8 + a2d1254 commit 55c5127

File tree

9 files changed

+68
-39
lines changed

9 files changed

+68
-39
lines changed

compiler/rustc_ast/src/attr/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ impl NestedMetaItem {
115115
pub fn is_meta_item_list(&self) -> bool {
116116
self.meta_item_list().is_some()
117117
}
118+
119+
pub fn name_value_literal_span(&self) -> Option<Span> {
120+
self.meta_item()?.name_value_literal_span()
121+
}
118122
}
119123

120124
impl Attribute {
@@ -175,6 +179,22 @@ impl Attribute {
175179
pub fn is_value_str(&self) -> bool {
176180
self.value_str().is_some()
177181
}
182+
183+
/// This is used in case you want the value span instead of the whole attribute. Example:
184+
///
185+
/// ```text
186+
/// #[doc(alias = "foo")]
187+
/// ```
188+
///
189+
/// In here, it'll return a span for `"foo"`.
190+
pub fn name_value_literal_span(&self) -> Option<Span> {
191+
match self.kind {
192+
AttrKind::Normal(ref item, _) => {
193+
item.meta(self.span).and_then(|meta| meta.name_value_literal_span())
194+
}
195+
AttrKind::DocComment(..) => None,
196+
}
197+
}
178198
}
179199

180200
impl MetaItem {
@@ -227,6 +247,17 @@ impl MetaItem {
227247
pub fn is_value_str(&self) -> bool {
228248
self.value_str().is_some()
229249
}
250+
251+
/// This is used in case you want the value span instead of the whole attribute. Example:
252+
///
253+
/// ```text
254+
/// #[doc(alias = "foo")]
255+
/// ```
256+
///
257+
/// In here, it'll return a span for `"foo"`.
258+
pub fn name_value_literal_span(&self) -> Option<Span> {
259+
Some(self.name_value_literal()?.span)
260+
}
230261
}
231262

232263
impl AttrItem {

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ where
294294
or \"none\"",
295295
)
296296
.span_label(
297-
mi.name_value_literal().unwrap().span,
297+
mi.name_value_literal_span().unwrap(),
298298
msg,
299299
)
300300
.emit();

compiler/rustc_expand/src/expand.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1603,23 +1603,22 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
16031603
items.push(ast::NestedMetaItem::MetaItem(item));
16041604
}
16051605
Err(e) => {
1606-
let lit =
1607-
it.meta_item().and_then(|item| item.name_value_literal()).unwrap();
1606+
let lit_span = it.name_value_literal_span().unwrap();
16081607

16091608
if e.kind() == ErrorKind::InvalidData {
16101609
self.cx
16111610
.struct_span_err(
1612-
lit.span,
1611+
lit_span,
16131612
&format!("{} wasn't a utf-8 file", filename.display()),
16141613
)
1615-
.span_label(lit.span, "contains invalid utf-8")
1614+
.span_label(lit_span, "contains invalid utf-8")
16161615
.emit();
16171616
} else {
16181617
let mut err = self.cx.struct_span_err(
1619-
lit.span,
1618+
lit_span,
16201619
&format!("couldn't read {}: {}", filename.display(), e),
16211620
);
1622-
err.span_label(lit.span, "couldn't read file");
1621+
err.span_label(lit_span, "couldn't read file");
16231622

16241623
err.emit();
16251624
}

compiler/rustc_middle/src/middle/limits.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ fn update_limit(
4343

4444
let value_span = attr
4545
.meta()
46-
.and_then(|meta| meta.name_value_literal().cloned())
47-
.map(|lit| lit.span)
46+
.and_then(|meta| meta.name_value_literal_span())
4847
.unwrap_or(attr.span);
4948

5049
let error_str = match e.kind() {

compiler/rustc_passes/src/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl CheckAttrVisitor<'tcx> {
319319
self.tcx
320320
.sess
321321
.struct_span_err(
322-
meta.span(),
322+
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
323323
&format!(
324324
"{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
325325
c,
@@ -332,7 +332,7 @@ impl CheckAttrVisitor<'tcx> {
332332
self.tcx
333333
.sess
334334
.struct_span_err(
335-
meta.span(),
335+
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
336336
"`#[doc(alias = \"...\")]` cannot start or end with ' '",
337337
)
338338
.emit();

src/test/rustdoc-ui/check-doc-alias-attr.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ LL | #[doc(alias("bar"))]
1717
| ^^^^^^^^^^^^
1818

1919
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
20-
--> $DIR/check-doc-alias-attr.rs:9:7
20+
--> $DIR/check-doc-alias-attr.rs:9:15
2121
|
2222
LL | #[doc(alias = "\"")]
23-
| ^^^^^^^^^^^^
23+
| ^^^^
2424

2525
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
26-
--> $DIR/check-doc-alias-attr.rs:10:7
26+
--> $DIR/check-doc-alias-attr.rs:10:15
2727
|
2828
LL | #[doc(alias = "\n")]
29-
| ^^^^^^^^^^^^
29+
| ^^^^
3030

3131
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
32-
--> $DIR/check-doc-alias-attr.rs:11:7
32+
--> $DIR/check-doc-alias-attr.rs:11:15
3333
|
3434
LL | #[doc(alias = "
35-
| _______^
35+
| _______________^
3636
LL | | ")]
3737
| |_^
3838

3939
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
40-
--> $DIR/check-doc-alias-attr.rs:13:7
40+
--> $DIR/check-doc-alias-attr.rs:13:15
4141
|
4242
LL | #[doc(alias = "\t")]
43-
| ^^^^^^^^^^^^
43+
| ^^^^
4444

4545
error: `#[doc(alias = "...")]` cannot start or end with ' '
46-
--> $DIR/check-doc-alias-attr.rs:14:7
46+
--> $DIR/check-doc-alias-attr.rs:14:15
4747
|
4848
LL | #[doc(alias = " hello")]
49-
| ^^^^^^^^^^^^^^^^
49+
| ^^^^^^^^
5050

5151
error: `#[doc(alias = "...")]` cannot start or end with ' '
52-
--> $DIR/check-doc-alias-attr.rs:15:7
52+
--> $DIR/check-doc-alias-attr.rs:15:15
5353
|
5454
LL | #[doc(alias = "hello ")]
55-
| ^^^^^^^^^^^^^^^^
55+
| ^^^^^^^^
5656

5757
error: aborting due to 9 previous errors
5858

src/test/rustdoc-ui/doc-alias-crate-level.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
2-
--> $DIR/doc-alias-crate-level.rs:5:7
2+
--> $DIR/doc-alias-crate-level.rs:5:15
33
|
44
LL | #[doc(alias = "shouldn't work!")]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^
66

77
error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute
88
--> $DIR/doc-alias-crate-level.rs:3:8

src/test/ui/check-doc-alias-attr.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ LL | #[doc(alias("bar"))]
1717
| ^^^^^^^^^^^^
1818

1919
error: '\"' character isn't allowed in `#[doc(alias = "...")]`
20-
--> $DIR/check-doc-alias-attr.rs:10:7
20+
--> $DIR/check-doc-alias-attr.rs:10:15
2121
|
2222
LL | #[doc(alias = "\"")]
23-
| ^^^^^^^^^^^^
23+
| ^^^^
2424

2525
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
26-
--> $DIR/check-doc-alias-attr.rs:11:7
26+
--> $DIR/check-doc-alias-attr.rs:11:15
2727
|
2828
LL | #[doc(alias = "\n")]
29-
| ^^^^^^^^^^^^
29+
| ^^^^
3030

3131
error: '\n' character isn't allowed in `#[doc(alias = "...")]`
32-
--> $DIR/check-doc-alias-attr.rs:12:7
32+
--> $DIR/check-doc-alias-attr.rs:12:15
3333
|
3434
LL | #[doc(alias = "
35-
| _______^
35+
| _______________^
3636
LL | | ")]
3737
| |_^
3838

3939
error: '\t' character isn't allowed in `#[doc(alias = "...")]`
40-
--> $DIR/check-doc-alias-attr.rs:14:7
40+
--> $DIR/check-doc-alias-attr.rs:14:15
4141
|
4242
LL | #[doc(alias = "\t")]
43-
| ^^^^^^^^^^^^
43+
| ^^^^
4444

4545
error: `#[doc(alias = "...")]` cannot start or end with ' '
46-
--> $DIR/check-doc-alias-attr.rs:15:7
46+
--> $DIR/check-doc-alias-attr.rs:15:15
4747
|
4848
LL | #[doc(alias = " hello")]
49-
| ^^^^^^^^^^^^^^^^
49+
| ^^^^^^^^
5050

5151
error: `#[doc(alias = "...")]` cannot start or end with ' '
52-
--> $DIR/check-doc-alias-attr.rs:16:7
52+
--> $DIR/check-doc-alias-attr.rs:16:15
5353
|
5454
LL | #[doc(alias = "hello ")]
55-
| ^^^^^^^^^^^^^^^^
55+
| ^^^^^^^^
5656

5757
error: aborting due to 9 previous errors
5858

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
2-
--> $DIR/doc-alias-crate-level.rs:7:8
2+
--> $DIR/doc-alias-crate-level.rs:7:16
33
|
44
LL | #![doc(alias = "shouldn't work!")]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)