Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve attribute message error spans #79509

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ impl NestedMetaItem {
pub fn is_meta_item_list(&self) -> bool {
self.meta_item_list().is_some()
}

pub fn name_value_literal_span(&self) -> Option<Span> {
self.meta_item()?.name_value_literal_span()
}
}

impl Attribute {
Expand Down Expand Up @@ -175,6 +179,22 @@ impl Attribute {
pub fn is_value_str(&self) -> bool {
self.value_str().is_some()
}

/// This is used in case you want the value span instead of the whole attribute. Example:
///
/// ```text
/// #[doc(alias = "foo")]
/// ```
///
/// In here, it'll return a span for `"foo"`.
pub fn name_value_literal_span(&self) -> Option<Span> {
match self.kind {
AttrKind::Normal(ref item, _) => {
item.meta(self.span).and_then(|meta| meta.name_value_literal_span())
}
AttrKind::DocComment(..) => None,
}
}
}

impl MetaItem {
Expand Down Expand Up @@ -227,6 +247,17 @@ impl MetaItem {
pub fn is_value_str(&self) -> bool {
self.value_str().is_some()
}

/// This is used in case you want the value span instead of the whole attribute. Example:
///
/// ```text
/// #[doc(alias = "foo")]
/// ```
///
/// In here, it'll return a span for `"foo"`.
pub fn name_value_literal_span(&self) -> Option<Span> {
Some(self.name_value_literal()?.span)
}
}

impl AttrItem {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ where
or \"none\"",
)
.span_label(
mi.name_value_literal().unwrap().span,
mi.name_value_literal_span().unwrap(),
msg,
)
.emit();
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,23 +1603,22 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
items.push(ast::NestedMetaItem::MetaItem(item));
}
Err(e) => {
let lit =
it.meta_item().and_then(|item| item.name_value_literal()).unwrap();
let lit_span = it.name_value_literal_span().unwrap();

if e.kind() == ErrorKind::InvalidData {
self.cx
.struct_span_err(
lit.span,
lit_span,
&format!("{} wasn't a utf-8 file", filename.display()),
)
.span_label(lit.span, "contains invalid utf-8")
.span_label(lit_span, "contains invalid utf-8")
.emit();
} else {
let mut err = self.cx.struct_span_err(
lit.span,
lit_span,
&format!("couldn't read {}: {}", filename.display(), e),
);
err.span_label(lit.span, "couldn't read file");
err.span_label(lit_span, "couldn't read file");

err.emit();
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/middle/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ fn update_limit(

let value_span = attr
.meta()
.and_then(|meta| meta.name_value_literal().cloned())
.map(|lit| lit.span)
.and_then(|meta| meta.name_value_literal_span())
.unwrap_or(attr.span);

let error_str = match e.kind() {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl CheckAttrVisitor<'tcx> {
self.tcx
.sess
.struct_span_err(
meta.span(),
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
&format!(
"{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
c,
Expand All @@ -332,7 +332,7 @@ impl CheckAttrVisitor<'tcx> {
self.tcx
.sess
.struct_span_err(
meta.span(),
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
"`#[doc(alias = \"...\")]` cannot start or end with ' '",
)
.emit();
Expand Down
24 changes: 12 additions & 12 deletions src/test/rustdoc-ui/check-doc-alias-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ LL | #[doc(alias("bar"))]
| ^^^^^^^^^^^^

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

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

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

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

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

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

error: aborting due to 9 previous errors

4 changes: 2 additions & 2 deletions src/test/rustdoc-ui/doc-alias-crate-level.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
--> $DIR/doc-alias-crate-level.rs:5:7
--> $DIR/doc-alias-crate-level.rs:5:15
|
LL | #[doc(alias = "shouldn't work!")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^

error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute
--> $DIR/doc-alias-crate-level.rs:3:8
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/check-doc-alias-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ LL | #[doc(alias("bar"))]
| ^^^^^^^^^^^^

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

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

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

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

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

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

error: aborting due to 9 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/doc-alias-crate-level.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: '\'' character isn't allowed in `#[doc(alias = "...")]`
--> $DIR/doc-alias-crate-level.rs:7:8
--> $DIR/doc-alias-crate-level.rs:7:16
|
LL | #![doc(alias = "shouldn't work!")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^

error: aborting due to previous error