Skip to content

Commit 4752a54

Browse files
committed
Disable using non-ascii identifiers in extern blocks.
1 parent 5c897d4 commit 4752a54

6 files changed

+80
-3
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,25 @@ impl<'a> AstValidator<'a> {
532532
}
533533
}
534534

535+
/// An item in `extern { ... }` cannot use non-ascii identifier.
536+
fn check_foreign_item_ascii_only(&self, ident: Ident) {
537+
let symbol_str = ident.as_str();
538+
if !symbol_str.is_ascii() {
539+
let n = 83942;
540+
self.err_handler()
541+
.struct_span_err(
542+
ident.span,
543+
"items in `extern` blocks cannot use non-ascii identifiers",
544+
)
545+
.span_label(self.current_extern_span(), "in this `extern` block")
546+
.note(&format!(
547+
"This limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
548+
n, n,
549+
))
550+
.emit();
551+
}
552+
}
553+
535554
/// Reject C-varadic type unless the function is foreign,
536555
/// or free and `unsafe extern "C"` semantically.
537556
fn check_c_varadic_type(&self, fk: FnKind<'a>) {
@@ -592,7 +611,7 @@ impl<'a> AstValidator<'a> {
592611
self.session,
593612
ident.span,
594613
E0754,
595-
"trying to load file for module `{}` with non ascii identifer name",
614+
"trying to load file for module `{}` with non-ascii identifier name",
596615
ident.name
597616
)
598617
.help("consider using `#[path]` attribute to specify filesystem path")
@@ -1103,15 +1122,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11031122
self.check_defaultness(fi.span, *def);
11041123
self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
11051124
self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
1125+
self.check_foreign_item_ascii_only(fi.ident);
11061126
}
11071127
ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => {
11081128
self.check_defaultness(fi.span, *def);
11091129
self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span));
11101130
self.check_type_no_bounds(bounds, "`extern` blocks");
11111131
self.check_foreign_ty_genericless(generics);
1132+
self.check_foreign_item_ascii_only(fi.ident);
11121133
}
11131134
ForeignItemKind::Static(_, _, body) => {
11141135
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1136+
self.check_foreign_item_ascii_only(fi.ident);
11151137
}
11161138
ForeignItemKind::MacCall(..) => {}
11171139
}

src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum Bär { //~ ERROR non-ascii idents
2828

2929
extern "C" {
3030
fn qüx(); //~ ERROR non-ascii idents
31+
//~^ ERROR items in `extern` blocks
3132
}
3233

3334
fn main() {}

src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
error: items in `extern` blocks cannot use non-ascii identifiers
2+
--> $DIR/feature-gate-non_ascii_idents.rs:30:8
3+
|
4+
LL | extern "C" {
5+
| ---------- in this `extern` block
6+
LL | fn qüx();
7+
| ^^^
8+
|
9+
= note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
10+
111
error[E0658]: non-ascii idents are not fully supported
212
--> $DIR/feature-gate-non_ascii_idents.rs:1:22
313
|
@@ -115,6 +125,6 @@ LL | fn qüx();
115125
= note: see issue #55467 <https://github.com/rust-lang/rust/issues/55467> for more information
116126
= help: add `#![feature(non_ascii_idents)]` to the crate attributes to enable
117127

118-
error: aborting due to 13 previous errors
128+
error: aborting due to 14 previous errors
119129

120130
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(extern_types)]
2+
#![feature(non_ascii_idents)]
3+
4+
extern "C" {
5+
type ; //~ items in `extern` blocks cannot use non-ascii identifiers
6+
fn (); //~ items in `extern` blocks cannot use non-ascii identifiers
7+
static: usize; //~ items in `extern` blocks cannot use non-ascii identifiers
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: items in `extern` blocks cannot use non-ascii identifiers
2+
--> $DIR/extern_block_nonascii_forbidden.rs:5:10
3+
|
4+
LL | extern "C" {
5+
| ---------- in this `extern` block
6+
LL | type 一;
7+
| ^^
8+
|
9+
= note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
10+
11+
error: items in `extern` blocks cannot use non-ascii identifiers
12+
--> $DIR/extern_block_nonascii_forbidden.rs:6:8
13+
|
14+
LL | extern "C" {
15+
| ---------- in this `extern` block
16+
LL | type 一;
17+
LL | fn 二();
18+
| ^^
19+
|
20+
= note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
21+
22+
error: items in `extern` blocks cannot use non-ascii identifiers
23+
--> $DIR/extern_block_nonascii_forbidden.rs:7:12
24+
|
25+
LL | extern "C" {
26+
| ---------- in this `extern` block
27+
...
28+
LL | static 三: usize;
29+
| ^^
30+
|
31+
= note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
32+
33+
error: aborting due to 3 previous errors
34+

src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | mod řųśť;
66
|
77
= help: to create the module `řųśť`, create file "$DIR/řųśť.rs"
88

9-
error[E0754]: trying to load file for module `řųśť` with non ascii identifer name
9+
error[E0754]: trying to load file for module `řųśť` with non-ascii identifier name
1010
--> $DIR/mod_file_nonascii_forbidden.rs:3:5
1111
|
1212
LL | mod řųśť;

0 commit comments

Comments
 (0)