Skip to content

Commit f4b542a

Browse files
authored
Unrolled build for #143181
Rollup merge of #143181 - JonathanBrouwer:malformed-attrs, r=oli-obk Improve testing and error messages for malformed attributes This PR has been split into 5 commits for reviewability, I recommend reviewing them one-by-one. This first commit introduces more tests, the other 4 commits fix 4 bugs discovered by the test. r? `@oli-obk` cc `@jdonszelmann` Fixes #143136
2 parents 4e97337 + 57a5e3b commit f4b542a

File tree

9 files changed

+858
-15
lines changed

9 files changed

+858
-15
lines changed

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
2121
span: cx.attr_span,
2222
reason: match args {
2323
ArgParser::NoArgs => None,
24-
ArgParser::NameValue(name_value) => name_value.value_as_str(),
24+
ArgParser::NameValue(name_value) => {
25+
let Some(value_str) = name_value.value_as_str() else {
26+
cx.expected_string_literal(
27+
name_value.value_span,
28+
Some(&name_value.value_as_lit()),
29+
);
30+
return None;
31+
};
32+
Some(value_str)
33+
}
2534
ArgParser::List(_) => {
2635
let suggestions =
2736
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
577577
diag.code(E0565);
578578
}
579579
AttributeParseErrorReason::ExpectedNameValue(None) => {
580-
diag.span_label(
581-
self.span,
582-
format!("expected this to be of the form `{name} = \"...\"`"),
583-
);
580+
// The suggestion we add below this match already contains enough information
584581
}
585582
AttributeParseErrorReason::ExpectedNameValue(Some(name)) => {
586583
diag.span_label(

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ fn emit_malformed_attribute(
303303
| sym::must_use
304304
| sym::track_caller
305305
| sym::link_name
306+
| sym::export_name
307+
| sym::rustc_macro_transparency
308+
| sym::link_section
309+
| sym::rustc_layout_scalar_valid_range_start
310+
| sym::rustc_layout_scalar_valid_range_end
306311
) {
307312
return;
308313
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// This file contains a bunch of malformed attributes.
2+
// We enable a bunch of features to not get feature-gate errs in this test.
3+
#![feature(rustc_attrs)]
4+
#![feature(rustc_allow_const_fn_unstable)]
5+
#![feature(allow_internal_unstable)]
6+
#![feature(fn_align)]
7+
#![feature(optimize_attribute)]
8+
#![feature(dropck_eyepatch)]
9+
#![feature(export_stable)]
10+
#![allow(incomplete_features)]
11+
#![feature(min_generic_const_args)]
12+
#![feature(ffi_const, ffi_pure)]
13+
#![feature(coverage_attribute)]
14+
#![feature(no_sanitize)]
15+
#![feature(marker_trait_attr)]
16+
#![feature(thread_local)]
17+
#![feature(must_not_suspend)]
18+
#![feature(coroutines)]
19+
#![feature(linkage)]
20+
#![feature(cfi_encoding, extern_types)]
21+
#![feature(patchable_function_entry)]
22+
#![feature(omit_gdb_pretty_printer_section)]
23+
#![feature(fundamental)]
24+
25+
26+
#![omit_gdb_pretty_printer_section = 1]
27+
//~^ ERROR malformed `omit_gdb_pretty_printer_section` attribute input
28+
29+
#![windows_subsystem]
30+
//~^ ERROR malformed
31+
32+
#[unsafe(export_name)]
33+
//~^ ERROR malformed
34+
#[rustc_allow_const_fn_unstable]
35+
//~^ ERROR `rustc_allow_const_fn_unstable` expects a list of feature names
36+
#[allow_internal_unstable]
37+
//~^ ERROR `allow_internal_unstable` expects a list of feature names
38+
#[rustc_confusables]
39+
//~^ ERROR malformed
40+
#[deprecated = 5]
41+
//~^ ERROR malformed
42+
#[doc]
43+
//~^ ERROR valid forms for the attribute are
44+
//~| WARN this was previously accepted by the compiler
45+
#[rustc_macro_transparency]
46+
//~^ ERROR malformed
47+
#[repr]
48+
//~^ ERROR malformed
49+
#[rustc_as_ptr = 5]
50+
//~^ ERROR malformed
51+
#[inline = 5]
52+
//~^ ERROR valid forms for the attribute are
53+
//~| WARN this was previously accepted by the compiler
54+
#[align]
55+
//~^ ERROR malformed
56+
#[optimize]
57+
//~^ ERROR malformed
58+
#[cold = 1]
59+
//~^ ERROR malformed
60+
#[must_use()]
61+
//~^ ERROR valid forms for the attribute are
62+
#[no_mangle = 1]
63+
//~^ ERROR malformed
64+
#[unsafe(naked())]
65+
//~^ ERROR malformed
66+
#[track_caller()]
67+
//~^ ERROR malformed
68+
#[export_name()]
69+
//~^ ERROR malformed
70+
#[used()]
71+
//~^ ERROR malformed
72+
#[crate_name]
73+
//~^ ERROR malformed
74+
#[doc]
75+
//~^ ERROR valid forms for the attribute are
76+
//~| WARN this was previously accepted by the compiler
77+
#[target_feature]
78+
//~^ ERROR malformed
79+
#[export_stable = 1]
80+
//~^ ERROR malformed
81+
#[link]
82+
//~^ ERROR attribute must be of the form
83+
//~| WARN this was previously accepted by the compiler
84+
#[link_name]
85+
//~^ ERROR malformed
86+
#[link_section]
87+
//~^ ERROR malformed
88+
#[coverage]
89+
//~^ ERROR malformed `coverage` attribute input
90+
#[no_sanitize]
91+
//~^ ERROR malformed
92+
#[ignore()]
93+
//~^ ERROR valid forms for the attribute are
94+
//~| WARN this was previously accepted by the compiler
95+
#[no_implicit_prelude = 23]
96+
//~^ ERROR malformed
97+
#[proc_macro = 18]
98+
//~^ ERROR malformed
99+
//~| ERROR the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
100+
#[cfg]
101+
//~^ ERROR is not followed by parentheses
102+
#[cfg_attr]
103+
//~^ ERROR malformed
104+
#[instruction_set]
105+
//~^ ERROR malformed
106+
#[patchable_function_entry]
107+
//~^ ERROR malformed
108+
fn test() {
109+
#[coroutine = 63] || {}
110+
//~^ ERROR malformed `coroutine` attribute input
111+
//~| ERROR mismatched types [E0308]
112+
}
113+
114+
#[proc_macro_attribute = 19]
115+
//~^ ERROR malformed
116+
//~| ERROR the `#[proc_macro_attribute]` attribute is only usable with crates of the `proc-macro` crate type
117+
#[must_use = 1]
118+
//~^ ERROR malformed
119+
fn test2() { }
120+
121+
#[proc_macro_derive]
122+
//~^ ERROR malformed `proc_macro_derive` attribute
123+
//~| ERROR the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type
124+
pub fn test3() {}
125+
126+
#[rustc_layout_scalar_valid_range_start]
127+
//~^ ERROR malformed
128+
#[rustc_layout_scalar_valid_range_end]
129+
//~^ ERROR malformed
130+
#[must_not_suspend()]
131+
//~^ ERROR malformed
132+
#[cfi_encoding]
133+
//~^ ERROR malformed
134+
struct Test;
135+
136+
#[diagnostic::on_unimplemented]
137+
//~^ WARN missing options for `on_unimplemented` attribute
138+
#[diagnostic::on_unimplemented = 1]
139+
//~^ WARN malformed
140+
trait Hey {
141+
#[type_const = 1]
142+
//~^ ERROR malformed
143+
const HEY: usize = 5;
144+
}
145+
146+
struct Empty;
147+
#[diagnostic::do_not_recommend()]
148+
//~^ WARN does not expect any arguments
149+
impl Hey for Empty {
150+
151+
}
152+
153+
#[marker = 3]
154+
//~^ ERROR malformed
155+
#[fundamental()]
156+
//~^ ERROR malformed
157+
trait EmptyTrait {
158+
159+
}
160+
161+
162+
extern "C" {
163+
#[unsafe(ffi_pure = 1)]
164+
//~^ ERROR malformed
165+
#[link_ordinal]
166+
//~^ ERROR malformed
167+
pub fn baz();
168+
169+
#[unsafe(ffi_const = 1)]
170+
//~^ ERROR malformed
171+
#[linkage]
172+
//~^ ERROR malformed
173+
pub fn bar();
174+
}
175+
176+
#[allow]
177+
//~^ ERROR malformed
178+
#[expect]
179+
//~^ ERROR malformed
180+
#[warn]
181+
//~^ ERROR malformed
182+
#[deny]
183+
//~^ ERROR malformed
184+
#[forbid]
185+
//~^ ERROR malformed
186+
#[debugger_visualizer]
187+
//~^ ERROR invalid argument
188+
//~| ERROR malformed `debugger_visualizer` attribute input
189+
#[automatically_derived = 18]
190+
//~^ ERROR malformed
191+
mod yooo {
192+
193+
}
194+
195+
#[non_exhaustive = 1]
196+
//~^ ERROR malformed
197+
enum Slenum {
198+
199+
}
200+
201+
#[thread_local()]
202+
//~^ ERROR malformed
203+
static mut TLS: u8 = 42;
204+
205+
#[no_link()]
206+
//~^ ERROR malformed
207+
#[macro_use = 1]
208+
//~^ ERROR malformed
209+
extern crate wloop;
210+
//~^ ERROR can't find crate for `wloop` [E0463]
211+
212+
#[macro_export = 18]
213+
//~^ ERROR malformed `macro_export` attribute input
214+
#[allow_internal_unsafe = 1]
215+
//~^ ERROR malformed
216+
//~| ERROR allow_internal_unsafe side-steps the unsafe_code lint
217+
macro_rules! slump {
218+
() => {}
219+
}
220+
221+
fn main() {}

0 commit comments

Comments
 (0)