Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e4599d

Browse files
committedMar 11, 2025
Improve -Z crate-attr diagnostics
- Move `crate-attr` parsing from `rustc_builtin_macros` to `rustc_parse`. This is not a built-in macro. - Show the `#![ ... ]` in the span (to make it clear that it should not be included in the CLI argument) - Show more detailed errors when the crate has valid token trees but invalid syntax. Previously, `crate-attr=feature(foo),feature(bar)` would just say "invalid crate attribute" and point at the comma. Now, it explicitly says that the comma was unexpected, which is useful when using `--error-format=short`. It also fixes the column to show the correct span. - Recover from parse errors. Previously we would abort immediately on syntax errors; now we go on to try and type-check the rest of the crate. The new diagnostic code also happens to be slightly shorter.
1 parent 518fa80 commit 5e4599d

File tree

13 files changed

+69
-80
lines changed

13 files changed

+69
-80
lines changed
 

‎compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ pub fn mk_doc_comment(
619619
Attribute { kind: AttrKind::DocComment(comment_kind, data), id: g.mk_attr_id(), style, span }
620620
}
621621

622-
pub fn mk_attr(
622+
fn mk_attr(
623623
g: &AttrIdGenerator,
624624
style: AttrStyle,
625625
unsafety: Safety,

‎compiler/rustc_builtin_macros/src/cmdline_attrs.rs

-44
This file was deleted.

‎compiler/rustc_builtin_macros/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ pub(crate) struct ProcMacro {
109109
pub(crate) span: Span,
110110
}
111111

112-
#[derive(Diagnostic)]
113-
#[diag(builtin_macros_invalid_crate_attribute)]
114-
pub(crate) struct InvalidCrateAttr {
115-
#[primary_span]
116-
pub(crate) span: Span,
117-
}
118-
119112
#[derive(Diagnostic)]
120113
#[diag(builtin_macros_non_abi)]
121114
pub(crate) struct NonABI {

‎compiler/rustc_builtin_macros/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ mod test;
5353
mod trace_macros;
5454

5555
pub mod asm;
56-
pub mod cmdline_attrs;
5756
pub mod contracts;
5857
pub mod proc_macro_harness;
5958
pub mod standard_library_imports;

‎compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
757757
) -> T {
758758
let sess = &compiler.sess;
759759

760-
rustc_builtin_macros::cmdline_attrs::inject(
760+
rustc_parse::cmdline_attrs::inject(
761761
&mut krate,
762762
&sess.psess,
763763
&sess.opts.unstable_opts.crate_attr,

‎compiler/rustc_parse/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub const MACRO_ARGUMENTS: Option<&str> = Some("macro arguments");
3232

3333
#[macro_use]
3434
pub mod parser;
35+
pub use parser::cmdline_attrs;
3536
use parser::{Parser, make_unclosed_delims_error};
3637
pub mod lexer;
3738
pub mod validate_attr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Attributes injected into the crate root from command line using `-Z crate-attr`.
2+
3+
use rustc_ast::{self as ast};
4+
use rustc_errors::Diag;
5+
use rustc_session::parse::ParseSess;
6+
use rustc_span::FileName;
7+
8+
use crate::{parse_in, source_str_to_stream};
9+
10+
use super::attr::InnerAttrPolicy;
11+
12+
pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
13+
for raw_attr in attrs {
14+
let source = format!("#![{raw_attr}]");
15+
let parse = || -> Result<ast::Attribute, Vec<Diag<'_>>> {
16+
let tokens = source_str_to_stream(psess, FileName::cli_crate_attr_source_code(raw_attr), source, None)?;
17+
parse_in(psess, tokens, "<crate attribute>", |p| p.parse_attribute(InnerAttrPolicy::Permitted)).map_err(|e| vec![e])
18+
};
19+
let meta = match parse() {
20+
Ok(meta) => meta,
21+
Err(errs) => {
22+
for err in errs {
23+
err.emit();
24+
}
25+
continue;
26+
}
27+
};
28+
29+
krate.attrs.push(meta);
30+
}
31+
}

‎compiler/rustc_parse/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod attr;
22
mod attr_wrapper;
3+
pub mod cmdline_attrs;
34
mod diagnostics;
45
mod expr;
56
mod generics;
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
error: unknown start of token: `
2-
--> <crate attribute>:1:1
2+
--> <crate attribute>:1:4
33
|
4-
LL | `%~@$#
5-
| ^
4+
LL | #![`%~@$#]
5+
| ^
66
|
77
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
88
|
9-
LL - `%~@$#
10-
LL + '%~@$#
9+
LL - #![`%~@$#]
10+
LL + #!['%~@$#]
1111
|
1212

1313
error: expected identifier, found `%`
14-
--> <crate attribute>:1:2
14+
--> <crate attribute>:1:5
1515
|
16-
LL | `%~@$#
17-
| ^ expected identifier
16+
LL | #![`%~@$#]
17+
| ^ expected identifier
1818

1919
error: aborting due to 2 previous errors
2020

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
error: unexpected closing delimiter: `]`
2-
--> <crate attribute>:1:19
1+
error: unexpected token: keyword `fn`
2+
--> <crate attribute>:1:23
33
|
4-
LL | feature(yeet_expr)]fn main(){}#[inline
5-
| ^ unexpected closing delimiter
4+
LL | #![feature(yeet_expr)]fn main(){}#[inline]
5+
| ^^ unexpected token after this
66

7-
error: aborting due to 1 previous error
7+
error[E0601]: `main` function not found in crate `injection`
8+
--> $DIR/injection.rs:3:12
9+
|
10+
LL | fn foo() {}
11+
| ^ consider adding a `main` function to `$DIR/injection.rs`
12+
13+
error: aborting due to 2 previous errors
814

15+
For more information about this error, try `rustc --explain E0601`.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: expected identifier, found `#`
2-
--> <crate attribute>:1:1
2+
--> <crate attribute>:1:4
33
|
4-
LL | #![feature(foo)]
5-
| ^ expected identifier
4+
LL | #![#![feature(foo)]]
5+
| ^ expected identifier
66

77
error: aborting due to 1 previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: invalid crate attribute
2-
--> <crate attribute>:1:1
1+
error: expected `]`, found `,`
2+
--> <crate attribute>:1:16
33
|
4-
LL | feature(foo),feature(bar)
5-
| ^^^^^^^^^^^^^
4+
LL | #![feature(foo),feature(bar)]
5+
| ^ expected `]`
66

77
error: aborting due to 1 previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: this file contains an unclosed delimiter
2-
--> <crate attribute>:1:2
1+
error: mismatched closing delimiter: `]`
2+
--> <crate attribute>:1:4
33
|
4-
LL | (
5-
| -^
6-
| |
7-
| unclosed delimiter
4+
LL | #![(]
5+
| -^^ mismatched closing delimiter
6+
| ||
7+
| |unclosed delimiter
8+
| closing delimiter possibly meant for this
89

910
error: aborting due to 1 previous error
1011

0 commit comments

Comments
 (0)
Failed to load comments.