Skip to content

Commit a6d5564

Browse files
authored
Unrolled build for rust-lang#138336
Rollup merge of rust-lang#138336 - jyn514:crate-attr-diagnostics, r=compiler-errors Improve `-Z crate-attr` diagnostics - 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. r? diagnostics
2 parents f836ae4 + d50a8d5 commit a6d5564

File tree

14 files changed

+82
-78
lines changed

14 files changed

+82
-78
lines changed

Diff for: compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ pub fn mk_doc_comment(
627627
Attribute { kind: AttrKind::DocComment(comment_kind, data), id: g.mk_attr_id(), style, span }
628628
}
629629

630-
pub fn mk_attr(
630+
fn mk_attr(
631631
g: &AttrIdGenerator,
632632
style: AttrStyle,
633633
unsafety: Safety,

Diff for: compiler/rustc_builtin_macros/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ builtin_macros_format_unused_args = multiple unused formatting arguments
231231
232232
builtin_macros_format_use_positional = consider using a positional formatting argument instead
233233
234-
builtin_macros_invalid_crate_attribute = invalid crate attribute
235-
236234
builtin_macros_multiple_default_attrs = multiple `#[default]` attributes
237235
.note = only one `#[default]` attribute is needed
238236
.label = `#[default]` used here

Diff for: compiler/rustc_builtin_macros/src/cmdline_attrs.rs

+25-32
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,37 @@
11
//! Attributes injected into the crate root from command line using `-Z crate-attr`.
22
3-
use rustc_ast::attr::mk_attr;
4-
use rustc_ast::{self as ast, AttrItem, AttrStyle, token};
5-
use rustc_parse::parser::ForceCollect;
6-
use rustc_parse::{new_parser_from_source_str, unwrap_or_emit_fatal};
3+
use rustc_ast::{self as ast};
4+
use rustc_errors::Diag;
5+
use rustc_parse::parser::attr::InnerAttrPolicy;
6+
use rustc_parse::{parse_in, source_str_to_stream};
77
use rustc_session::parse::ParseSess;
88
use rustc_span::FileName;
99

10-
use crate::errors;
11-
1210
pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
1311
for raw_attr in attrs {
14-
let mut parser = unwrap_or_emit_fatal(new_parser_from_source_str(
15-
psess,
16-
FileName::cli_crate_attr_source_code(raw_attr),
17-
raw_attr.clone(),
18-
));
19-
20-
let start_span = parser.token.span;
21-
let AttrItem { unsafety, path, args, tokens: _ } =
22-
match parser.parse_attr_item(ForceCollect::No) {
23-
Ok(ai) => ai,
24-
Err(err) => {
12+
let source = format!("#![{raw_attr}]");
13+
let parse = || -> Result<ast::Attribute, Vec<Diag<'_>>> {
14+
let tokens = source_str_to_stream(
15+
psess,
16+
FileName::cli_crate_attr_source_code(raw_attr),
17+
source,
18+
None,
19+
)?;
20+
parse_in(psess, tokens, "<crate attribute>", |p| {
21+
p.parse_attribute(InnerAttrPolicy::Permitted)
22+
})
23+
.map_err(|e| vec![e])
24+
};
25+
let meta = match parse() {
26+
Ok(meta) => meta,
27+
Err(errs) => {
28+
for err in errs {
2529
err.emit();
26-
continue;
2730
}
28-
};
29-
let end_span = parser.token.span;
30-
if parser.token != token::Eof {
31-
psess.dcx().emit_err(errors::InvalidCrateAttr { span: start_span.to(end_span) });
32-
continue;
33-
}
31+
continue;
32+
}
33+
};
3434

35-
krate.attrs.push(mk_attr(
36-
&psess.attr_id_generator,
37-
AttrStyle::Inner,
38-
unsafety,
39-
path,
40-
args,
41-
start_span.to(end_span),
42-
));
35+
krate.attrs.push(meta);
4336
}
4437
}

Diff for: 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 {

Diff for: tests/ui/attributes/z-crate-attr/garbage.stderr

+8-8
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

Diff for: tests/ui/attributes/z-crate-attr/injection.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
//@ compile-flags: '-Zcrate-attr=feature(yeet_expr)]fn main(){}#[inline'
2-
3-
fn foo() {}
4-
5-
//~? ERROR unexpected closing delimiter: `]`
2+
//~? ERROR unexpected token
3+
fn foo() {} //~ ERROR `main` function not found

Diff for: tests/ui/attributes/z-crate-attr/injection.stderr

+12-5
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`.

Diff for: tests/ui/attributes/z-crate-attr/injection2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//@ compile-flags: -Zcrate-attr=feature(yeet_expr)]#![allow(warnings)
2+
//~? ERROR unexpected token
3+
fn foo() {} //~ ERROR `main` function not found

Diff for: tests/ui/attributes/z-crate-attr/injection2.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unexpected token: `#`
2+
--> <crate attribute>:1:23
3+
|
4+
LL | #![feature(yeet_expr)]#![allow(warnings)]
5+
| ^ unexpected token after this
6+
7+
error[E0601]: `main` function not found in crate `injection2`
8+
--> $DIR/injection2.rs:3:12
9+
|
10+
LL | fn foo() {}
11+
| ^ consider adding a `main` function to `$DIR/injection2.rs`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0601`.

Diff for: tests/ui/attributes/z-crate-attr/inner-attr.stderr

+3-3
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

Diff for: tests/ui/attributes/z-crate-attr/multiple.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
//@ compile-flags: -Zcrate-attr=feature(foo),feature(bar)
2-
2+
//~? ERROR expected `]`
33
fn main() {}
4-
5-
//~? ERROR invalid crate attribute

Diff for: tests/ui/attributes/z-crate-attr/multiple.stderr

+4-4
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

Diff for: tests/ui/attributes/z-crate-attr/unbalanced-paren.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// Show diagnostics for unbalanced parens.
22
//@ compile-flags: -Zcrate-attr=(
3-
3+
//~? ERROR mismatched closing delimiter
44
fn main() {}
5-
6-
//~? ERROR this file contains an unclosed delimiter
+7-6
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)