Skip to content

Commit 7c7cb08

Browse files
committed
rewrite objc attribute parser errors to assume standard library macro use case
1 parent 43c34c4 commit 7c7cb08

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ attr_parsing_null_on_export = `export_name` may not contain null characters
122122
123123
attr_parsing_null_on_link_section = `link_section` may not contain null characters
124124
125-
attr_parsing_null_on_objc_class = `rustc_objc_class` may not contain null characters
125+
attr_parsing_null_on_objc_class = `objc::class!` may not contain null characters
126126
127-
attr_parsing_null_on_objc_selector = `rustc_objc_selector` may not contain null characters
127+
attr_parsing_null_on_objc_selector = `objc::selector!` may not contain null characters
128+
129+
attr_parsing_objc_class_expected_string_literal = `objc::class!` expected a string literal
130+
131+
attr_parsing_objc_selector_expected_string_literal = `objc::selector!` expected a string literal
128132
129133
attr_parsing_repr_ident =
130134
meta item in `repr` must be an identifier

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_session::parse::feature_err;
44
use super::prelude::*;
55
use crate::session_diagnostics::{
66
NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector,
7+
ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral,
78
};
89

910
pub(crate) struct OptimizeParser;
@@ -168,13 +169,13 @@ impl<S: Stage> SingleAttributeParser<S> for ObjcClassParser {
168169
return None;
169170
};
170171
let Some(classname) = nv.value_as_str() else {
171-
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
172+
cx.emit_err(ObjcClassExpectedStringLiteral { span: nv.value_span });
172173
return None;
173174
};
174175
if classname.as_str().contains('\0') {
175176
// `#[rustc_objc_class = ...]` will be converted to a null-terminated string,
176177
// so it may not contain any null characters.
177-
cx.emit_err(NullOnObjcClass { span: cx.attr_span });
178+
cx.emit_err(NullOnObjcClass { span: nv.value_span });
178179
return None;
179180
}
180181
Some(AttributeKind::ObjcClass { classname, span: cx.attr_span })
@@ -197,13 +198,13 @@ impl<S: Stage> SingleAttributeParser<S> for ObjcSelectorParser {
197198
return None;
198199
};
199200
let Some(methname) = nv.value_as_str() else {
200-
cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
201+
cx.emit_err(ObjcSelectorExpectedStringLiteral { span: nv.value_span });
201202
return None;
202203
};
203204
if methname.as_str().contains('\0') {
204205
// `#[rustc_objc_selector = ...]` will be converted to a null-terminated string,
205206
// so it may not contain any null characters.
206-
cx.emit_err(NullOnObjcSelector { span: cx.attr_span });
207+
cx.emit_err(NullOnObjcSelector { span: nv.value_span });
207208
return None;
208209
}
209210
Some(AttributeKind::ObjcSelector { methname, span: cx.attr_span })

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,20 @@ pub(crate) struct NullOnObjcSelector {
473473
pub span: Span,
474474
}
475475

476+
#[derive(Diagnostic)]
477+
#[diag(attr_parsing_objc_class_expected_string_literal)]
478+
pub(crate) struct ObjcClassExpectedStringLiteral {
479+
#[primary_span]
480+
pub span: Span,
481+
}
482+
483+
#[derive(Diagnostic)]
484+
#[diag(attr_parsing_objc_selector_expected_string_literal)]
485+
pub(crate) struct ObjcSelectorExpectedStringLiteral {
486+
#[primary_span]
487+
pub span: Span,
488+
}
489+
476490
#[derive(Diagnostic)]
477491
#[diag(attr_parsing_stability_outside_std, code = E0734)]
478492
pub(crate) struct StabilityOutsideStd {

tests/ui/darwin-objc/darwin-objc-bad-arg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub fn main() {
1616
//~^ ERROR attribute value must be a literal
1717

1818
objc::class!(123);
19-
//~^ ERROR malformed `rustc_objc_class` attribute input [E0539]
19+
//~^ ERROR `objc::class!` expected a string literal
2020

2121
objc::class!("NSObject\0");
22-
//~^ ERROR `rustc_objc_class` may not contain null characters
22+
//~^ ERROR `objc::class!` may not contain null characters
2323

2424
let s = "alloc";
2525
objc::selector!(s);
@@ -29,8 +29,8 @@ pub fn main() {
2929
//~^ ERROR attribute value must be a literal
3030

3131
objc::selector!(123);
32-
//~^ ERROR malformed `rustc_objc_selector` attribute input [E0539]
32+
//~^ ERROR `objc::selector!` expected a string literal
3333

3434
objc::selector!("alloc\0");
35-
//~^ ERROR `rustc_objc_selector` may not contain null characters
35+
//~^ ERROR `objc::selector!` may not contain null characters
3636
}

tests/ui/darwin-objc/darwin-objc-bad-arg.stderr

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,17 @@ error: attribute value must be a literal
1010
LL | objc::class!(NSObject);
1111
| ^^^^^^^^
1212

13-
WARN rustc_errors::emitter Invalid span $SRC_DIR/core/src/os/darwin/objc.rs:LL:COL (#6), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/os/darwin/objc.rs" }) }
14-
error[E0539]: malformed `rustc_objc_class` attribute input
15-
--> $DIR/darwin-objc-bad-arg.rs:18:5
13+
error: `objc::class!` expected a string literal
14+
--> $DIR/darwin-objc-bad-arg.rs:18:18
1615
|
1716
LL | objc::class!(123);
18-
| ^^^^^^^^^^^^^---^
19-
| | |
20-
| | expected a string literal here
21-
| help: must be of the form: `#[rustc_objc_class = "ClassName"]`
22-
|
23-
= note: this error originates in the macro `objc::class` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
| ^^^
2418

25-
error: `rustc_objc_class` may not contain null characters
26-
--> $DIR/darwin-objc-bad-arg.rs:21:5
19+
error: `objc::class!` may not contain null characters
20+
--> $DIR/darwin-objc-bad-arg.rs:21:18
2721
|
2822
LL | objc::class!("NSObject\0");
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
|
31-
= note: this error originates in the macro `objc::class` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
| ^^^^^^^^^^^^
3224

3325
error: attribute value must be a literal
3426
--> $DIR/darwin-objc-bad-arg.rs:25:21
@@ -42,26 +34,17 @@ error: attribute value must be a literal
4234
LL | objc::selector!(alloc);
4335
| ^^^^^
4436

45-
WARN rustc_errors::emitter Invalid span $SRC_DIR/core/src/os/darwin/objc.rs:LL:COL (#10), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/os/darwin/objc.rs" }) }
46-
error[E0539]: malformed `rustc_objc_selector` attribute input
47-
--> $DIR/darwin-objc-bad-arg.rs:31:5
37+
error: `objc::selector!` expected a string literal
38+
--> $DIR/darwin-objc-bad-arg.rs:31:21
4839
|
4940
LL | objc::selector!(123);
50-
| ^^^^^^^^^^^^^^^^---^
51-
| | |
52-
| | expected a string literal here
53-
| help: must be of the form: `#[rustc_objc_selector = "methodName"]`
54-
|
55-
= note: this error originates in the macro `objc::selector` (in Nightly builds, run with -Z macro-backtrace for more info)
41+
| ^^^
5642

57-
error: `rustc_objc_selector` may not contain null characters
58-
--> $DIR/darwin-objc-bad-arg.rs:34:5
43+
error: `objc::selector!` may not contain null characters
44+
--> $DIR/darwin-objc-bad-arg.rs:34:21
5945
|
6046
LL | objc::selector!("alloc\0");
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
62-
|
63-
= note: this error originates in the macro `objc::selector` (in Nightly builds, run with -Z macro-backtrace for more info)
47+
| ^^^^^^^^^
6448

6549
error: aborting due to 8 previous errors
6650

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

0 commit comments

Comments
 (0)