Skip to content

Commit 8a51a4b

Browse files
committed
Using expected_specific_argument_strings for reporting malformed input
1 parent 7d6bfb1 commit 8a51a4b

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ attr_parsing_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
8686
.suggestion = remove the `unsafe(...)`
8787
.note = extraneous unsafe is not allowed in attributes
8888
89-
attr_parsing_invalid_export_visibility =
90-
invalid export visibility: {$unrecognized_visibility}
91-
9289
attr_parsing_invalid_issue_string =
9390
`issue` must be a non-zero numeric string or "none"
9491
.must_not_be_zero = `issue` must not be "0", use "none" instead

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use std::str::FromStr;
2-
31
use rustc_hir::attrs::{
42
CoverageAttrKind, ExportVisibilityAttrValue, OptimizeAttr, RtsanSetting, SanitizerSet, UsedBy,
53
};
64
use rustc_session::parse::feature_err;
75

86
use super::prelude::*;
97
use crate::session_diagnostics::{
10-
InvalidExportVisibility, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass,
11-
NullOnObjcSelector, ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
8+
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
9+
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
1210
};
1311
use crate::target_checking::Policy::AllowSilent;
1412

@@ -176,14 +174,21 @@ impl<S: Stage> SingleAttributeParser<S> for ExportVisibilityParser {
176174
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
177175
return None;
178176
};
179-
let Ok(visibility) = ExportVisibilityAttrValue::from_str(sv.as_str()) else {
180-
cx.emit_err(InvalidExportVisibility {
181-
span: nv.value_span,
182-
unrecognized_visibility: sv.to_string(),
183-
});
184-
return None;
185-
};
186-
Some(AttributeKind::ExportVisibility { visibility, span: cx.attr_span })
177+
178+
let str_to_visibility = [("target_default", ExportVisibilityAttrValue::TargetDefault)];
179+
for &(s, visibility) in str_to_visibility.iter() {
180+
if s == sv.as_str() {
181+
return Some(AttributeKind::ExportVisibility { visibility, span: cx.attr_span });
182+
}
183+
}
184+
185+
let allowed_str_values = str_to_visibility
186+
.into_iter()
187+
.map(|(s, _visibility)| s)
188+
.map(Symbol::intern)
189+
.collect::<Vec<_>>();
190+
cx.expected_specific_argument_strings(nv.value_span, &allowed_str_values);
191+
None
187192
}
188193
}
189194

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,6 @@ pub(crate) struct UnusedMultiple {
389389
pub name: Symbol,
390390
}
391391

392-
#[derive(Diagnostic)]
393-
#[diag(attr_parsing_invalid_export_visibility)]
394-
pub(crate) struct InvalidExportVisibility {
395-
#[primary_span]
396-
pub span: Span,
397-
pub unrecognized_visibility: String,
398-
}
399-
400392
#[derive(Diagnostic)]
401393
#[diag(attr_parsing_null_on_export, code = E0648)]
402394
pub(crate) struct NullOnExport {

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::borrow::Cow;
22
use std::path::PathBuf;
3-
use std::str::FromStr;
43

54
pub use ReprAttr::*;
65
use rustc_abi::Align;
@@ -197,17 +196,6 @@ pub enum ExportVisibilityAttrValue {
197196
TargetDefault,
198197
}
199198

200-
impl FromStr for ExportVisibilityAttrValue {
201-
type Err = ();
202-
203-
fn from_str(s: &str) -> Result<ExportVisibilityAttrValue, Self::Err> {
204-
match s {
205-
"target_default" => Ok(ExportVisibilityAttrValue::TargetDefault),
206-
_ => Err(()),
207-
}
208-
}
209-
}
210-
211199
/// There are three valid forms of the attribute:
212200
/// `#[used]`, which is equivalent to `#[used(linker)]` on targets that support it, but `#[used(compiler)]` if not.
213201
/// `#[used(compiler)]`

tests/ui/attributes/export-visibility-with-unrecognized-arg.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
#![feature(export_visibility)]
44
#[no_mangle]
55
#[export_visibility = "unrecognized visibility value"]
6-
//~^ ERROR: invalid export visibility
6+
//~^ ERROR: malformed `export_visibility` attribute input
77
pub static TESTED_STATIC: [u8; 6] = *b"foobar";
88

99
// The following `static`s verify that `hidden`, `protected`, and `interposable`
1010
// are not supported yet.
1111
#[no_mangle]
1212
#[export_visibility = "hidden"]
13-
//~^ ERROR: invalid export visibility
13+
//~^ ERROR: malformed `export_visibility` attribute input
1414
pub static TESTED_STATIC_HIDDEN: [u8; 6] = *b"foobar";
1515
#[no_mangle]
1616
#[export_visibility = "protected"]
17-
//~^ ERROR: invalid export visibility
17+
//~^ ERROR: malformed `export_visibility` attribute input
1818
pub static TESTED_STATIC_PROTECTED: [u8; 6] = *b"foobar";
1919
#[no_mangle]
2020
#[export_visibility = "interposable"]
21-
//~^ ERROR: invalid export visibility
21+
//~^ ERROR: malformed `export_visibility` attribute input
2222
pub static TESTED_STATIC_INTERPOSABLE: [u8; 6] = *b"foobar";
2323

2424
// The following `static`s verify that other visibility spellings are also not supported.
2525
#[no_mangle]
2626
#[export_visibility = "default"]
27-
//~^ ERROR: invalid export visibility
27+
//~^ ERROR: malformed `export_visibility` attribute input
2828
pub static TESTED_STATIC_DEFAULT: [u8; 6] = *b"foobar";
2929
#[no_mangle]
3030
#[export_visibility = "public"]
31-
//~^ ERROR: invalid export visibility
31+
//~^ ERROR: malformed `export_visibility` attribute input
3232
pub static TESTED_STATIC_PUBLIC: [u8; 6] = *b"foobar";
3333

3434
fn main() {}
Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1-
error: invalid export visibility: unrecognized visibility value
2-
--> $DIR/export-visibility-with-unrecognized-arg.rs:5:23
1+
error[E0539]: malformed `export_visibility` attribute input
2+
--> $DIR/export-visibility-with-unrecognized-arg.rs:5:1
33
|
44
LL | #[export_visibility = "unrecognized visibility value"]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^-------------------------------^
6+
| | |
7+
| | the only valid argument here is "target_default"
8+
| help: must be of the form: `#[export_visibility = "visibility"]`
69

7-
error: invalid export visibility: hidden
8-
--> $DIR/export-visibility-with-unrecognized-arg.rs:12:23
10+
error[E0539]: malformed `export_visibility` attribute input
11+
--> $DIR/export-visibility-with-unrecognized-arg.rs:12:1
912
|
1013
LL | #[export_visibility = "hidden"]
11-
| ^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^^^^--------^
15+
| | |
16+
| | the only valid argument here is "target_default"
17+
| help: must be of the form: `#[export_visibility = "visibility"]`
1218

13-
error: invalid export visibility: protected
14-
--> $DIR/export-visibility-with-unrecognized-arg.rs:16:23
19+
error[E0539]: malformed `export_visibility` attribute input
20+
--> $DIR/export-visibility-with-unrecognized-arg.rs:16:1
1521
|
1622
LL | #[export_visibility = "protected"]
17-
| ^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^-----------^
24+
| | |
25+
| | the only valid argument here is "target_default"
26+
| help: must be of the form: `#[export_visibility = "visibility"]`
1827

19-
error: invalid export visibility: interposable
20-
--> $DIR/export-visibility-with-unrecognized-arg.rs:20:23
28+
error[E0539]: malformed `export_visibility` attribute input
29+
--> $DIR/export-visibility-with-unrecognized-arg.rs:20:1
2130
|
2231
LL | #[export_visibility = "interposable"]
23-
| ^^^^^^^^^^^^^^
32+
| ^^^^^^^^^^^^^^^^^^^^^^--------------^
33+
| | |
34+
| | the only valid argument here is "target_default"
35+
| help: must be of the form: `#[export_visibility = "visibility"]`
2436

25-
error: invalid export visibility: default
26-
--> $DIR/export-visibility-with-unrecognized-arg.rs:26:23
37+
error[E0539]: malformed `export_visibility` attribute input
38+
--> $DIR/export-visibility-with-unrecognized-arg.rs:26:1
2739
|
2840
LL | #[export_visibility = "default"]
29-
| ^^^^^^^^^
41+
| ^^^^^^^^^^^^^^^^^^^^^^---------^
42+
| | |
43+
| | the only valid argument here is "target_default"
44+
| help: must be of the form: `#[export_visibility = "visibility"]`
3045

31-
error: invalid export visibility: public
32-
--> $DIR/export-visibility-with-unrecognized-arg.rs:30:23
46+
error[E0539]: malformed `export_visibility` attribute input
47+
--> $DIR/export-visibility-with-unrecognized-arg.rs:30:1
3348
|
3449
LL | #[export_visibility = "public"]
35-
| ^^^^^^^^
50+
| ^^^^^^^^^^^^^^^^^^^^^^--------^
51+
| | |
52+
| | the only valid argument here is "target_default"
53+
| help: must be of the form: `#[export_visibility = "visibility"]`
3654

3755
error: aborting due to 6 previous errors
3856

57+
For more information about this error, try `rustc --explain E0539`.

0 commit comments

Comments
 (0)