From 9613a88db5fd8bf3a882be35a9e8e87075e41bea Mon Sep 17 00:00:00 2001 From: Camelid Date: Sat, 13 Mar 2021 13:13:27 -0800 Subject: [PATCH 1/8] Refactor `check_doc_attrs` body This change makes it easier to follow the control flow. I also moved the end-of-line comments attached to some symbols to before the symbol listing. This allows rustfmt to format the code; otherwise no formatting occurs (see rust-lang/rustfmt#4750). --- compiler/rustc_passes/src/check_attr.rs | 77 +++++++++++++------------ 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c7b266f18bf..a045b0d596d 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -531,60 +531,65 @@ impl CheckAttrVisitor<'tcx> { } fn check_doc_attrs(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool { - if let Some(mi) = attr.meta() { - if let Some(list) = mi.meta_item_list() { - for meta in list { - if meta.has_name(sym::alias) { - if !self.check_attr_crate_level(meta, hir_id, "alias") - || !self.check_doc_alias(meta, hir_id, target) + if let Some(list) = attr.meta().and_then(|mi| mi.meta_item_list().map(|l| l.to_vec())) { + for meta in list { + if let Some(i_meta) = meta.meta_item() { + match i_meta.name_or_empty() { + sym::alias + if !self.check_attr_crate_level(&meta, hir_id, "alias") + || !self.check_doc_alias(&meta, hir_id, target) => { return false; } - } else if meta.has_name(sym::keyword) { - if !self.check_attr_crate_level(meta, hir_id, "keyword") - || !self.check_doc_keyword(meta, hir_id) + + sym::keyword + if !self.check_attr_crate_level(&meta, hir_id, "keyword") + || !self.check_doc_keyword(&meta, hir_id) => { return false; } - } else if meta.has_name(sym::test) { - if CRATE_HIR_ID != hir_id { + + sym::test if CRATE_HIR_ID != hir_id => { self.tcx.struct_span_lint_hir( INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| { lint.build( - "`#![doc(test(...)]` is only allowed as a crate level attribute" + "`#![doc(test(...)]` is only allowed \ + as a crate level attribute", ) .emit(); }, ); return false; } - } else if let Some(i_meta) = meta.meta_item() { - if ![ - sym::cfg, - sym::hidden, - sym::html_favicon_url, - sym::html_logo_url, - sym::html_no_source, - sym::html_playground_url, - sym::html_root_url, - sym::include, - sym::inline, - sym::issue_tracker_base_url, - sym::masked, - sym::no_default_passes, // deprecated - sym::no_inline, - sym::passes, // deprecated - sym::plugins, // removed, but rustdoc warns about it itself - sym::primitive, - sym::spotlight, - sym::test, - ] - .iter() - .any(|m| i_meta.has_name(*m)) - { + + // no_default_passes: deprecated + // passes: deprecated + // plugins: removed, but rustdoc warns about it itself + sym::alias + | sym::cfg + | sym::hidden + | sym::html_favicon_url + | sym::html_logo_url + | sym::html_no_source + | sym::html_playground_url + | sym::html_root_url + | sym::include + | sym::inline + | sym::issue_tracker_base_url + | sym::keyword + | sym::masked + | sym::no_default_passes + | sym::no_inline + | sym::passes + | sym::plugins + | sym::primitive + | sym::spotlight + | sym::test => {} + + _ => { self.tcx.struct_span_lint_hir( INVALID_DOC_ATTRIBUTES, hir_id, From 7189c05bf8fe5b9d21815c44540d76301c90a8aa Mon Sep 17 00:00:00 2001 From: Camelid Date: Sat, 13 Mar 2021 13:25:27 -0800 Subject: [PATCH 2/8] Lint non-meta doc attributes E.g., `#[doc(123)]`. --- compiler/rustc_passes/src/check_attr.rs | 10 ++++++++++ src/test/ui/attributes/doc-attr.rs | 8 ++++++++ src/test/ui/attributes/doc-attr.stderr | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index a045b0d596d..5120edb2e35 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -605,6 +605,16 @@ impl CheckAttrVisitor<'tcx> { return false; } } + } else { + self.tcx.struct_span_lint_hir( + INVALID_DOC_ATTRIBUTES, + hir_id, + meta.span(), + |lint| { + lint.build(&format!("unknown `doc` attribute")).emit(); + }, + ); + return false; } } } diff --git a/src/test/ui/attributes/doc-attr.rs b/src/test/ui/attributes/doc-attr.rs index 3a584112973..261ba75dfcb 100644 --- a/src/test/ui/attributes/doc-attr.rs +++ b/src/test/ui/attributes/doc-attr.rs @@ -8,3 +8,11 @@ //~^ ERROR unknown `doc` attribute //~^^ WARN pub fn foo() {} + +#[doc(123)] +//~^ ERROR unknown `doc` attribute +//~| WARN +#[doc("hello", "bar")] +//~^ ERROR unknown `doc` attribute +//~| WARN +fn bar() {} diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index 21479d25fc2..6bff40a7f70 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -13,6 +13,24 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:12:7 + | +LL | #[doc(123)] + | ^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:15:7 + | +LL | #[doc("hello", "bar")] + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + error: unknown `doc` attribute `as_ptr` --> $DIR/doc-attr.rs:3:8 | @@ -22,5 +40,5 @@ LL | #![doc(as_ptr)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors From 7e972a39b8e9c7228ce230206326e3e4c4e81e2c Mon Sep 17 00:00:00 2001 From: Camelid Date: Thu, 11 Mar 2021 15:41:51 -0800 Subject: [PATCH 3/8] Report error for each invalid nested attribute --- compiler/rustc_passes/src/check_attr.rs | 15 +++++++++------ src/test/ui/attributes/doc-attr.rs | 2 ++ src/test/ui/attributes/doc-attr.stderr | 11 ++++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 5120edb2e35..a1871e796d8 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -531,6 +531,8 @@ impl CheckAttrVisitor<'tcx> { } fn check_doc_attrs(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool { + let mut is_valid = true; + if let Some(list) = attr.meta().and_then(|mi| mi.meta_item_list().map(|l| l.to_vec())) { for meta in list { if let Some(i_meta) = meta.meta_item() { @@ -539,14 +541,14 @@ impl CheckAttrVisitor<'tcx> { if !self.check_attr_crate_level(&meta, hir_id, "alias") || !self.check_doc_alias(&meta, hir_id, target) => { - return false; + is_valid = false } sym::keyword if !self.check_attr_crate_level(&meta, hir_id, "keyword") || !self.check_doc_keyword(&meta, hir_id) => { - return false; + is_valid = false } sym::test if CRATE_HIR_ID != hir_id => { @@ -562,7 +564,7 @@ impl CheckAttrVisitor<'tcx> { .emit(); }, ); - return false; + is_valid = false; } // no_default_passes: deprecated @@ -602,7 +604,7 @@ impl CheckAttrVisitor<'tcx> { .emit(); }, ); - return false; + is_valid = false; } } } else { @@ -614,11 +616,12 @@ impl CheckAttrVisitor<'tcx> { lint.build(&format!("unknown `doc` attribute")).emit(); }, ); - return false; + is_valid = false; } } } - true + + is_valid } /// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid. diff --git a/src/test/ui/attributes/doc-attr.rs b/src/test/ui/attributes/doc-attr.rs index 261ba75dfcb..1b037985a18 100644 --- a/src/test/ui/attributes/doc-attr.rs +++ b/src/test/ui/attributes/doc-attr.rs @@ -15,4 +15,6 @@ pub fn foo() {} #[doc("hello", "bar")] //~^ ERROR unknown `doc` attribute //~| WARN +//~| ERROR unknown `doc` attribute +//~| WARN fn bar() {} diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index 6bff40a7f70..51714d2da86 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -31,6 +31,15 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:15:16 + | +LL | #[doc("hello", "bar")] + | ^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + error: unknown `doc` attribute `as_ptr` --> $DIR/doc-attr.rs:3:8 | @@ -40,5 +49,5 @@ LL | #![doc(as_ptr)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors From fe64970ed10230376b08c5d4187f9d204295383f Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 12 Mar 2021 12:27:52 -0800 Subject: [PATCH 4/8] Add another test case --- src/test/ui/attributes/doc-attr.rs | 5 +++++ src/test/ui/attributes/doc-attr.stderr | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/test/ui/attributes/doc-attr.rs b/src/test/ui/attributes/doc-attr.rs index 1b037985a18..daf73dd023c 100644 --- a/src/test/ui/attributes/doc-attr.rs +++ b/src/test/ui/attributes/doc-attr.rs @@ -17,4 +17,9 @@ pub fn foo() {} //~| WARN //~| ERROR unknown `doc` attribute //~| WARN +#[doc(foo::bar, crate::bar::baz = "bye")] +//~^ ERROR unknown `doc` attribute +//~| WARN +//~| ERROR unknown `doc` attribute +//~| WARN fn bar() {} diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index 51714d2da86..a2831770943 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -40,6 +40,24 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 +error: unknown `doc` attribute `` + --> $DIR/doc-attr.rs:20:7 + | +LL | #[doc(foo::bar, crate::bar::baz = "bye")] + | ^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute `` + --> $DIR/doc-attr.rs:20:17 + | +LL | #[doc(foo::bar, crate::bar::baz = "bye")] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + error: unknown `doc` attribute `as_ptr` --> $DIR/doc-attr.rs:3:8 | @@ -49,5 +67,5 @@ LL | #![doc(as_ptr)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: aborting due to 5 previous errors +error: aborting due to 7 previous errors From 5134047c4073de04749ddee31623318dbcaaffc1 Mon Sep 17 00:00:00 2001 From: Camelid Date: Sat, 13 Mar 2021 14:32:06 -0800 Subject: [PATCH 5/8] Add hyphen to "crate level" "crate level attribute" -> "crate-level attribute" --- compiler/rustc_passes/src/check_attr.rs | 4 ++-- src/test/rustdoc-ui/doc-alias-crate-level.stderr | 2 +- src/test/rustdoc-ui/doc-attr2.stderr | 4 ++-- src/test/ui/attributes/doc-attr2.stderr | 4 ++-- src/test/ui/rustdoc/doc-alias-crate-level.stderr | 2 +- src/test/ui/rustdoc/doc_keyword.stderr | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index a1871e796d8..c89518df007 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -520,7 +520,7 @@ impl CheckAttrVisitor<'tcx> { .struct_span_err( meta.span(), &format!( - "`#![doc({} = \"...\")]` isn't allowed as a crate level attribute", + "`#![doc({} = \"...\")]` isn't allowed as a crate-level attribute", attr_name, ), ) @@ -559,7 +559,7 @@ impl CheckAttrVisitor<'tcx> { |lint| { lint.build( "`#![doc(test(...)]` is only allowed \ - as a crate level attribute", + as a crate-level attribute", ) .emit(); }, diff --git a/src/test/rustdoc-ui/doc-alias-crate-level.stderr b/src/test/rustdoc-ui/doc-alias-crate-level.stderr index 9e746cba05f..fc8095e03ca 100644 --- a/src/test/rustdoc-ui/doc-alias-crate-level.stderr +++ b/src/test/rustdoc-ui/doc-alias-crate-level.stderr @@ -4,7 +4,7 @@ error: '\'' character isn't allowed in `#[doc(alias = "...")]` LL | #[doc(alias = "shouldn't work!")] | ^^^^^^^^^^^^^^^^^ -error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute +error: `#![doc(alias = "...")]` isn't allowed as a crate-level attribute --> $DIR/doc-alias-crate-level.rs:1:8 | LL | #![doc(alias = "crate-level-not-working")] diff --git a/src/test/rustdoc-ui/doc-attr2.stderr b/src/test/rustdoc-ui/doc-attr2.stderr index eeb2c2be085..643107318b9 100644 --- a/src/test/rustdoc-ui/doc-attr2.stderr +++ b/src/test/rustdoc-ui/doc-attr2.stderr @@ -1,4 +1,4 @@ -error: `#![doc(test(...)]` is only allowed as a crate level attribute +error: `#![doc(test(...)]` is only allowed as a crate-level attribute --> $DIR/doc-attr2.rs:4:7 | LL | #[doc(test(no_crate_inject))] @@ -13,7 +13,7 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: `#![doc(test(...)]` is only allowed as a crate level attribute +error: `#![doc(test(...)]` is only allowed as a crate-level attribute --> $DIR/doc-attr2.rs:9:12 | LL | #![doc(test(no_crate_inject))] diff --git a/src/test/ui/attributes/doc-attr2.stderr b/src/test/ui/attributes/doc-attr2.stderr index eeb2c2be085..643107318b9 100644 --- a/src/test/ui/attributes/doc-attr2.stderr +++ b/src/test/ui/attributes/doc-attr2.stderr @@ -1,4 +1,4 @@ -error: `#![doc(test(...)]` is only allowed as a crate level attribute +error: `#![doc(test(...)]` is only allowed as a crate-level attribute --> $DIR/doc-attr2.rs:4:7 | LL | #[doc(test(no_crate_inject))] @@ -13,7 +13,7 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: `#![doc(test(...)]` is only allowed as a crate level attribute +error: `#![doc(test(...)]` is only allowed as a crate-level attribute --> $DIR/doc-attr2.rs:9:12 | LL | #![doc(test(no_crate_inject))] diff --git a/src/test/ui/rustdoc/doc-alias-crate-level.stderr b/src/test/ui/rustdoc/doc-alias-crate-level.stderr index c0467514ae1..bd32609ade2 100644 --- a/src/test/ui/rustdoc/doc-alias-crate-level.stderr +++ b/src/test/ui/rustdoc/doc-alias-crate-level.stderr @@ -4,7 +4,7 @@ error: '\'' character isn't allowed in `#[doc(alias = "...")]` LL | #[doc(alias = "shouldn't work!")] | ^^^^^^^^^^^^^^^^^ -error: `#![doc(alias = "...")]` isn't allowed as a crate level attribute +error: `#![doc(alias = "...")]` isn't allowed as a crate-level attribute --> $DIR/doc-alias-crate-level.rs:5:8 | LL | #![doc(alias = "not working!")] diff --git a/src/test/ui/rustdoc/doc_keyword.stderr b/src/test/ui/rustdoc/doc_keyword.stderr index d72a876163e..0679bb8c5a7 100644 --- a/src/test/ui/rustdoc/doc_keyword.stderr +++ b/src/test/ui/rustdoc/doc_keyword.stderr @@ -10,7 +10,7 @@ error: `#[doc(keyword = "...")]` can only be used on modules LL | #[doc(keyword = "hall")] | ^^^^^^^^^^^^^^^^ -error: `#![doc(keyword = "...")]` isn't allowed as a crate level attribute +error: `#![doc(keyword = "...")]` isn't allowed as a crate-level attribute --> $DIR/doc_keyword.rs:4:8 | LL | #![doc(keyword = "hello")] From 13884dc2af3d993629eec4a5489dc99bd0a00a71 Mon Sep 17 00:00:00 2001 From: Camelid Date: Sat, 13 Mar 2021 16:36:38 -0800 Subject: [PATCH 6/8] Update `rustdoc-ui` versions of the `doc-attr` test It seems there are two copies of it: one in `src/test/ui/attributes/` and one in `src/test/rustdoc-ui/`. I'm guessing this is to test that the lint is emitted both when you run the compiler and when you run rustdoc. --- src/test/rustdoc-ui/doc-attr.rs | 15 +++++++++ src/test/rustdoc-ui/doc-attr.stderr | 47 ++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/test/rustdoc-ui/doc-attr.rs b/src/test/rustdoc-ui/doc-attr.rs index 3a584112973..daf73dd023c 100644 --- a/src/test/rustdoc-ui/doc-attr.rs +++ b/src/test/rustdoc-ui/doc-attr.rs @@ -8,3 +8,18 @@ //~^ ERROR unknown `doc` attribute //~^^ WARN pub fn foo() {} + +#[doc(123)] +//~^ ERROR unknown `doc` attribute +//~| WARN +#[doc("hello", "bar")] +//~^ ERROR unknown `doc` attribute +//~| WARN +//~| ERROR unknown `doc` attribute +//~| WARN +#[doc(foo::bar, crate::bar::baz = "bye")] +//~^ ERROR unknown `doc` attribute +//~| WARN +//~| ERROR unknown `doc` attribute +//~| WARN +fn bar() {} diff --git a/src/test/rustdoc-ui/doc-attr.stderr b/src/test/rustdoc-ui/doc-attr.stderr index 21479d25fc2..a2831770943 100644 --- a/src/test/rustdoc-ui/doc-attr.stderr +++ b/src/test/rustdoc-ui/doc-attr.stderr @@ -13,6 +13,51 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:12:7 + | +LL | #[doc(123)] + | ^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:15:7 + | +LL | #[doc("hello", "bar")] + | ^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute + --> $DIR/doc-attr.rs:15:16 + | +LL | #[doc("hello", "bar")] + | ^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute `` + --> $DIR/doc-attr.rs:20:7 + | +LL | #[doc(foo::bar, crate::bar::baz = "bye")] + | ^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + +error: unknown `doc` attribute `` + --> $DIR/doc-attr.rs:20:17 + | +LL | #[doc(foo::bar, crate::bar::baz = "bye")] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #82730 + error: unknown `doc` attribute `as_ptr` --> $DIR/doc-attr.rs:3:8 | @@ -22,5 +67,5 @@ LL | #![doc(as_ptr)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: aborting due to 2 previous errors +error: aborting due to 7 previous errors From 13076f90d2febedd44d395561b0ec844cac64f8b Mon Sep 17 00:00:00 2001 From: Camelid Date: Sun, 14 Mar 2021 14:00:02 -0700 Subject: [PATCH 7/8] Tweak diagnostics - Tweak lint message - Display multi-segment paths correctly --- compiler/rustc_passes/src/check_attr.rs | 15 +++++++++------ src/test/rustdoc-ui/doc-attr.rs | 6 +++--- src/test/rustdoc-ui/doc-attr.stderr | 10 +++++----- src/test/ui/attributes/doc-attr.rs | 6 +++--- src/test/ui/attributes/doc-attr.stderr | 10 +++++----- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index c89518df007..2edc2315f05 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -597,11 +597,14 @@ impl CheckAttrVisitor<'tcx> { hir_id, i_meta.span, |lint| { - lint.build(&format!( - "unknown `doc` attribute `{}`", - i_meta.name_or_empty() - )) - .emit(); + let msg = if let Ok(snippet) = + self.tcx.sess.source_map().span_to_snippet(i_meta.path.span) + { + format!("unknown `doc` attribute `{}`", snippet,) + } else { + String::from("unknown `doc` attribute") + }; + lint.build(&msg).emit(); }, ); is_valid = false; @@ -613,7 +616,7 @@ impl CheckAttrVisitor<'tcx> { hir_id, meta.span(), |lint| { - lint.build(&format!("unknown `doc` attribute")).emit(); + lint.build(&format!("invalid `doc` attribute")).emit(); }, ); is_valid = false; diff --git a/src/test/rustdoc-ui/doc-attr.rs b/src/test/rustdoc-ui/doc-attr.rs index daf73dd023c..980d1c0e207 100644 --- a/src/test/rustdoc-ui/doc-attr.rs +++ b/src/test/rustdoc-ui/doc-attr.rs @@ -10,12 +10,12 @@ pub fn foo() {} #[doc(123)] -//~^ ERROR unknown `doc` attribute +//~^ ERROR invalid `doc` attribute //~| WARN #[doc("hello", "bar")] -//~^ ERROR unknown `doc` attribute +//~^ ERROR invalid `doc` attribute //~| WARN -//~| ERROR unknown `doc` attribute +//~| ERROR invalid `doc` attribute //~| WARN #[doc(foo::bar, crate::bar::baz = "bye")] //~^ ERROR unknown `doc` attribute diff --git a/src/test/rustdoc-ui/doc-attr.stderr b/src/test/rustdoc-ui/doc-attr.stderr index a2831770943..cc2494c92e6 100644 --- a/src/test/rustdoc-ui/doc-attr.stderr +++ b/src/test/rustdoc-ui/doc-attr.stderr @@ -13,7 +13,7 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:12:7 | LL | #[doc(123)] @@ -22,7 +22,7 @@ LL | #[doc(123)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:15:7 | LL | #[doc("hello", "bar")] @@ -31,7 +31,7 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:15:16 | LL | #[doc("hello", "bar")] @@ -40,7 +40,7 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute `` +error: unknown `doc` attribute `foo::bar` --> $DIR/doc-attr.rs:20:7 | LL | #[doc(foo::bar, crate::bar::baz = "bye")] @@ -49,7 +49,7 @@ LL | #[doc(foo::bar, crate::bar::baz = "bye")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute `` +error: unknown `doc` attribute `crate::bar::baz` --> $DIR/doc-attr.rs:20:17 | LL | #[doc(foo::bar, crate::bar::baz = "bye")] diff --git a/src/test/ui/attributes/doc-attr.rs b/src/test/ui/attributes/doc-attr.rs index daf73dd023c..980d1c0e207 100644 --- a/src/test/ui/attributes/doc-attr.rs +++ b/src/test/ui/attributes/doc-attr.rs @@ -10,12 +10,12 @@ pub fn foo() {} #[doc(123)] -//~^ ERROR unknown `doc` attribute +//~^ ERROR invalid `doc` attribute //~| WARN #[doc("hello", "bar")] -//~^ ERROR unknown `doc` attribute +//~^ ERROR invalid `doc` attribute //~| WARN -//~| ERROR unknown `doc` attribute +//~| ERROR invalid `doc` attribute //~| WARN #[doc(foo::bar, crate::bar::baz = "bye")] //~^ ERROR unknown `doc` attribute diff --git a/src/test/ui/attributes/doc-attr.stderr b/src/test/ui/attributes/doc-attr.stderr index a2831770943..cc2494c92e6 100644 --- a/src/test/ui/attributes/doc-attr.stderr +++ b/src/test/ui/attributes/doc-attr.stderr @@ -13,7 +13,7 @@ LL | #![deny(warnings)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:12:7 | LL | #[doc(123)] @@ -22,7 +22,7 @@ LL | #[doc(123)] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:15:7 | LL | #[doc("hello", "bar")] @@ -31,7 +31,7 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute +error: invalid `doc` attribute --> $DIR/doc-attr.rs:15:16 | LL | #[doc("hello", "bar")] @@ -40,7 +40,7 @@ LL | #[doc("hello", "bar")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute `` +error: unknown `doc` attribute `foo::bar` --> $DIR/doc-attr.rs:20:7 | LL | #[doc(foo::bar, crate::bar::baz = "bye")] @@ -49,7 +49,7 @@ LL | #[doc(foo::bar, crate::bar::baz = "bye")] = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #82730 -error: unknown `doc` attribute `` +error: unknown `doc` attribute `crate::bar::baz` --> $DIR/doc-attr.rs:20:17 | LL | #[doc(foo::bar, crate::bar::baz = "bye")] From 8f40e1180f65bdf7e88baa4bcc03d24baded9fca Mon Sep 17 00:00:00 2001 From: Camelid Date: Sun, 14 Mar 2021 14:39:13 -0700 Subject: [PATCH 8/8] Use pretty-printer instead of `span_to_snippet` --- Cargo.lock | 1 + compiler/rustc_passes/Cargo.toml | 1 + compiler/rustc_passes/src/check_attr.rs | 11 ++++------- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25039b5cbd9..ceb3bdc6b22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4268,6 +4268,7 @@ name = "rustc_passes" version = "0.0.0" dependencies = [ "rustc_ast", + "rustc_ast_pretty", "rustc_attr", "rustc_data_structures", "rustc_errors", diff --git a/compiler/rustc_passes/Cargo.toml b/compiler/rustc_passes/Cargo.toml index c87799f1c2a..4069fb2127e 100644 --- a/compiler/rustc_passes/Cargo.toml +++ b/compiler/rustc_passes/Cargo.toml @@ -19,3 +19,4 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_lexer = { path = "../rustc_lexer" } +rustc_ast_pretty = { path = "../rustc_ast_pretty" } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 2edc2315f05..98771c91958 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -597,13 +597,10 @@ impl CheckAttrVisitor<'tcx> { hir_id, i_meta.span, |lint| { - let msg = if let Ok(snippet) = - self.tcx.sess.source_map().span_to_snippet(i_meta.path.span) - { - format!("unknown `doc` attribute `{}`", snippet,) - } else { - String::from("unknown `doc` attribute") - }; + let msg = format!( + "unknown `doc` attribute `{}`", + rustc_ast_pretty::pprust::path_to_string(&i_meta.path), + ); lint.build(&msg).emit(); }, );