Skip to content

Commit 3f4c44c

Browse files
authored
Unrolled build for #157025
Rollup merge of #157025 - Dnreikronos:fix/crate-root-path-help-in-attr-style-diagnostic, r=JonathanBrouwer Show crate root path in misplaced crate-level attribute diagnostic Fixes #157001 If you put `#![feature(...)]` inside a non-root module, the diagnostic tells you it can only be used at the crate root, but never says where the crate root actually is. Not great if you're new to Rust or working in a big workspace with nested modules. Now the diagnostic includes a help line with the crate root file path when the misplaced attribute is an inner attribute. Outer attributes already get a span note pointing at what they're applied to, so they don't need this. Path goes through `RemapPathScopeComponents::DIAGNOSTICS` so `--remap-path-prefix` is respected. ``` warning: the `#![feature]` attribute can only be used at the crate root --> src/foo.rs:1:1 | LL | #![feature(test)] | ^^^^^^^^^^^^^^^^^ | = help: the crate root is at `src/main.rs` ```
2 parents 8f02e85 + 8db956b commit 3f4c44c

5 files changed

Lines changed: 44 additions & 1 deletion

File tree

compiler/rustc_attr_parsing/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ pub(crate) struct InvalidAttrStyle {
165165
#[note("this attribute does not have an `!`, which means it is applied to this {$target}")]
166166
pub target_span: Option<Span>,
167167
pub target: &'static str,
168+
pub crate_root_path: String,
169+
#[help("the crate root is at `{$crate_root_path}`")]
170+
pub show_crate_root_help: bool,
168171
}
169172

170173
#[derive(Diagnostic)]

compiler/rustc_attr_parsing/src/target_checking.rs

Lines changed: 17 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, Span, Symbol, sym};
8+
use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};
99

1010
use crate::AttributeParser;
1111
use crate::context::AcceptContext;
@@ -186,13 +186,29 @@ impl<'sess> AttributeParser<'sess> {
186186
let target_span = cx.target_span;
187187
let attr_span = cx.attr_span;
188188

189+
let (show_crate_root_help, crate_root_path) = is_used_as_inner
190+
.then(|| cx.cx.sess.local_crate_source_file())
191+
.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+
})
198+
.map(|src| {
199+
(true, src.path(RemapPathScopeComponents::DIAGNOSTICS).display().to_string())
200+
})
201+
.unwrap_or_default();
202+
189203
cx.emit_lint(
190204
rustc_session::lint::builtin::UNUSED_ATTRIBUTES,
191205
crate::errors::InvalidAttrStyle {
192206
name,
193207
is_used_as_inner,
194208
target_span: (!is_used_as_inner).then_some(target_span),
195209
target: target.name(),
210+
crate_root_path,
211+
show_crate_root_help,
196212
},
197213
attr_span,
198214
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#![crate_name = "bar"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![deny(unused_attributes)]
2+
3+
#[path = "auxiliary/submod.rs"]
4+
mod submod;
5+
6+
fn main() {}
7+
8+
//~? 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/auxiliary/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+

0 commit comments

Comments
 (0)