Skip to content

Commit 1e62f5a

Browse files
committed
Suppress crate-root help when attribute is in same file
Skip the "the crate root is at ..." help message when the misplaced crate-level attribute is already in the crate root file, since it adds no useful information in that case. Add a cross-file test to verify the help still appears when the attribute is in a different file from the crate root.
1 parent 6a51d20 commit 1e62f5a

9 files changed

Lines changed: 160 additions & 162 deletions

File tree

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan, StashKey};
55
use rustc_feature::Features;
66
use rustc_hir::attrs::AttributeKind;
77
use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
8-
use rustc_span::{BytePos, RemapPathScopeComponents, Span, Symbol, sym};
8+
use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};
99

1010
use crate::AttributeParser;
1111
use crate::context::AcceptContext;
@@ -189,6 +189,12 @@ impl<'sess> AttributeParser<'sess> {
189189
let (show_crate_root_help, crate_root_path) = is_used_as_inner
190190
.then(|| cx.cx.sess.local_crate_source_file())
191191
.flatten()
192+
.filter(|src| {
193+
!matches!(
194+
cx.cx.sess.source_map().span_to_filename(attr_span),
195+
FileName::Real(ref name) if name == src
196+
)
197+
})
192198
.map(|src| {
193199
(true, src.path(RemapPathScopeComponents::DIAGNOSTICS).display().to_string())
194200
})

tests/ui/attributes/attr-mix-new.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ warning: the `#![feature]` attribute can only be used at the crate root
44
LL | #![feature(globs)]
55
| ^^^^^^^^^^^^^^^^^^
66
|
7-
= help: the crate root is at `$DIR/attr-mix-new.rs`
87
= note: requested on the command line with `-W unused-attributes`
98

109
warning: 1 warning emitted

tests/ui/attributes/crate-only-as-outer.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ error: the `#![crate_name]` attribute can only be used at the crate root
2121
|
2222
LL | #![crate_name = "iwi"]
2323
| ^^^^^^^^^^^^^^^^^^^^^^
24-
|
25-
= help: the crate root is at `$DIR/crate-only-as-outer.rs`
2624

2725
error: aborting due to 2 previous errors
2826

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![deny(unused_attributes)]
2+
3+
mod submod;
4+
5+
fn main() {}
6+
7+
//~? ERROR the `#![crate_name]` attribute can only be used at the crate root
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: the `#![crate_name]` attribute can only be used at the crate root
2+
--> $DIR/submod.rs:1:1
3+
|
4+
LL | #![crate_name = "bar"]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: the crate root is at `$DIR/crate-root-path-in-different-file.rs`
8+
note: the lint level is defined here
9+
--> $DIR/crate-root-path-in-different-file.rs:1:9
10+
|
11+
LL | #![deny(unused_attributes)]
12+
| ^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to 1 previous error
15+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#![crate_name = "bar"]

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ mod no_std {
503503
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
504504
mod inner { #![no_std] }
505505
//~^ WARN the `#![no_std]` attribute can only be used at the crate root
506-
//~| HELP the crate root is at
507506

508507
#[no_std] fn f() { }
509508
//~^ WARN crate-level attribute should be an inner attribute
@@ -765,8 +764,7 @@ mod windows_subsystem {
765764
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
766765
mod inner { #![windows_subsystem="windows"] }
767766
//~^ WARN the `#![windows_subsystem]` attribute can only be used at the crate root
768-
//~| HELP the crate root is at
769-
767+
770768
#[windows_subsystem = "windows"] fn f() { }
771769
//~^ WARN crate-level attribute should be an inner attribute
772770
//~| NOTE this attribute does not have an `!`, which means it is applied to this function
@@ -792,7 +790,6 @@ mod crate_name {
792790
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
793791
mod inner { #![crate_name="0900"] }
794792
//~^ WARN the `#![crate_name]` attribute can only be used at the crate root
795-
//~| HELP the crate root is at
796793

797794
#[crate_name = "0900"] fn f() { }
798795
//~^ WARN crate-level attribute should be an inner attribute
@@ -817,7 +814,6 @@ mod crate_type {
817814
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
818815
mod inner { #![crate_type="0800"] }
819816
//~^ WARN the `#![crate_type]` attribute can only be used at the crate root
820-
//~| HELP the crate root is at
821817

822818
#[crate_type = "0800"] fn f() { }
823819
//~^ WARN crate-level attribute should be an inner attribute
@@ -842,8 +838,7 @@ mod feature {
842838
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
843839
mod inner { #![feature(x0600)] }
844840
//~^ WARN the `#![feature]` attribute can only be used at the crate root
845-
//~| HELP the crate root is at
846-
841+
847842
#[feature(x0600)] fn f() { }
848843
//~^ WARN crate-level attribute should be an inner attribute
849844
//~| NOTE this attribute does not have an `!`, which means it is applied to this function
@@ -868,8 +863,7 @@ mod no_main_1 {
868863
//~^ NOTE: this attribute does not have an `!`, which means it is applied to this module
869864
mod inner { #![no_main] }
870865
//~^ WARN the `#![no_main]` attribute can only be used at the crate root
871-
//~| HELP the crate root is at
872-
866+
873867
#[no_main] fn f() { }
874868
//~^ WARN crate-level attribute should be an inner attribute
875869
//~| NOTE this attribute does not have an `!`, which means it is applied to this function
@@ -893,8 +887,7 @@ mod no_builtins {
893887
//~^ NOTE: this attribute does not have an `!`, which means it is applied to this module
894888
mod inner { #![no_builtins] }
895889
//~^ WARN the `#![no_builtins]` attribute can only be used at the crate root
896-
//~| HELP the crate root is at
897-
890+
898891
#[no_builtins] fn f() { }
899892
//~^ WARN crate-level attribute should be an inner attribute
900893
//~| NOTE this attribute does not have an `!`, which means it is applied to this function
@@ -918,7 +911,6 @@ mod recursion_limit {
918911
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
919912
mod inner { #![recursion_limit="0200"] }
920913
//~^ WARN the `#![recursion_limit]` attribute can only be used at the crate root
921-
//~| HELP the crate root is at
922914

923915
#[recursion_limit="0200"] fn f() { }
924916
//~^ WARN crate-level attribute should be an inner attribute
@@ -943,7 +935,6 @@ mod type_length_limit {
943935
//~^ NOTE this attribute does not have an `!`, which means it is applied to this module
944936
mod inner { #![type_length_limit="0100"] }
945937
//~^ WARN the `#![type_length_limit]` attribute can only be used at the crate root
946-
//~| HELP the crate root is at
947938

948939
#[type_length_limit="0100"] fn f() { }
949940
//~^ WARN crate-level attribute should be an inner attribute

0 commit comments

Comments
 (0)