Skip to content

Disallow forbidden usage of non-ascii identifiers. #72259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,35 @@ impl<'a> AstValidator<'a> {
.emit();
}

fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
if ident.name.as_str().is_ascii() {
return;
}
let head_span = self.session.source_map().guess_head_span(item_span);
struct_span_err!(
self.session,
head_span,
E0754,
"`#[no_mangle]` requires ASCII identifier"
)
.emit();
}

fn check_mod_file_item_asciionly(&self, ident: Ident) {
if ident.name.as_str().is_ascii() {
return;
}
struct_span_err!(
self.session,
ident.span,
E0754,
"trying to load file for module `{}` with non ascii identifer name",
ident.name
)
.help("consider using `#[path]` attribute to specify filesystem path")
.emit();
}

fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
if !generics.params.is_empty() {
struct_span_err!(
Expand Down Expand Up @@ -866,6 +895,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.has_proc_macro_decls = true;
}

if attr::contains_name(&item.attrs, sym::no_mangle) {
self.check_nomangle_item_asciionly(item.ident, item.span);
}

match item.kind {
ItemKind::Impl {
unsafety,
Expand Down Expand Up @@ -992,9 +1025,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
walk_list!(self, visit_attribute, &item.attrs);
return;
}
ItemKind::Mod(_) => {
ItemKind::Mod(Mod { inline, .. }) => {
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
attr::first_attr_value_str_by_name(&item.attrs, sym::path);
if !inline && !attr::contains_name(&item.attrs, sym::path) {
self.check_mod_file_item_asciionly(item.ident);
}
}
ItemKind::Union(ref vdata, _) => {
if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_error_codes/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ E0750: include_str!("./error_codes/E0750.md"),
E0751: include_str!("./error_codes/E0751.md"),
E0752: include_str!("./error_codes/E0752.md"),
E0753: include_str!("./error_codes/E0753.md"),
E0754: include_str!("./error_codes/E0754.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard
Expand Down
33 changes: 33 additions & 0 deletions src/librustc_error_codes/error_codes/E0754.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
An non-ascii identifier was used in an invalid context.

Erroneous code example:

```compile_fail,E0754
# #![feature(non_ascii_idents)]

mod řųśť;
// ^ error!
fn main() {}
```

```compile_fail,E0754
# #![feature(non_ascii_idents)]

#[no_mangle]
fn řųśť() {}
// ^ error!
fn main() {}
```

Non-ascii can be used as module names if it is inline
or a #\[path\] attribute is specified. For example:

```
# #![feature(non_ascii_idents)]

mod řųśť {
const IS_GREAT: bool = true;
}

fn main() {}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub trait Foo {}
6 changes: 6 additions & 0 deletions src/test/ui/rfc-2457/mod_file_nonascii_forbidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(non_ascii_idents)]

mod řųśť; //~ trying to load file for
//~^ file not found for

fn main() {}
20 changes: 20 additions & 0 deletions src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0583]: file not found for module `řųśť`
--> $DIR/mod_file_nonascii_forbidden.rs:3:1
|
LL | mod řųśť;
| ^^^^^^^^^
|
= help: to create the module `řųśť`, create file "$DIR/řųśť.rs"

error[E0754]: trying to load file for module `řųśť` with non ascii identifer name
--> $DIR/mod_file_nonascii_forbidden.rs:3:5
|
LL | mod řųśť;
| ^^^^
|
= help: consider using `#[path]` attribute to specify filesystem path

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0583, E0754.
For more information about an error, try `rustc --explain E0583`.
7 changes: 7 additions & 0 deletions src/test/ui/rfc-2457/mod_file_nonascii_with_path_allowed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass
#![feature(non_ascii_idents)]

#[path="auxiliary/mod_file_nonascii_with_path_allowed-aux.rs"]
mod řųśť;

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/rfc-2457/mod_inline_nonascii_allowed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
#![feature(non_ascii_idents)]

mod řųśť {
const IS_GREAT: bool = true;
}

fn main() {}
6 changes: 6 additions & 0 deletions src/test/ui/rfc-2457/no_mangle_nonascii_forbidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(non_ascii_idents)]

#[no_mangle]
pub fn řųśť() {} //~ `#[no_mangle]` requires ASCII identifier

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/rfc-2457/no_mangle_nonascii_forbidden.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0754]: `#[no_mangle]` requires ASCII identifier
--> $DIR/no_mangle_nonascii_forbidden.rs:4:1
|
LL | pub fn řųśť() {}
| ^^^^^^^^^^^^^

error: aborting due to previous error

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