about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2021-04-07 13:07:16 +0200
committerGitHub <noreply@github.com>2021-04-07 13:07:16 +0200
commit9c688cd2a2935ae4645c0b878f1f40ea51e98816 (patch)
tree5ef9ffad9d388273325d5340df4b3a94700dbdae
parentd554385bbf94c063220319b856bfa7b6d9f63aaf (diff)
parent4752a54ad00f4e1e8307958b80d0ba8258e93f1b (diff)
downloadrust-9c688cd2a2935ae4645c0b878f1f40ea51e98816.tar.gz
rust-9c688cd2a2935ae4645c0b878f1f40ea51e98816.zip
Rollup merge of #83936 - crlf0710:disallow_extern_block_non_ascii, r=Manishearth
Disable using non-ascii identifiers in extern blocks.

Fixes #83923.
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs24
-rw-r--r--src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs1
-rw-r--r--src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr12
-rw-r--r--src/test/ui/rfc-2457/extern_block_nonascii_forbidden.rs10
-rw-r--r--src/test/ui/rfc-2457/extern_block_nonascii_forbidden.stderr34
-rw-r--r--src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr2
6 files changed, 80 insertions, 3 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index 96bb9cfb1a6..bb09f701531 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -532,6 +532,25 @@ impl<'a> AstValidator<'a> {
         }
     }
 
+    /// An item in `extern { ... }` cannot use non-ascii identifier.
+    fn check_foreign_item_ascii_only(&self, ident: Ident) {
+        let symbol_str = ident.as_str();
+        if !symbol_str.is_ascii() {
+            let n = 83942;
+            self.err_handler()
+                .struct_span_err(
+                    ident.span,
+                    "items in `extern` blocks cannot use non-ascii identifiers",
+                )
+                .span_label(self.current_extern_span(), "in this `extern` block")
+                .note(&format!(
+                    "This limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
+                    n, n,
+                ))
+                .emit();
+        }
+    }
+
     /// Reject C-varadic type unless the function is foreign,
     /// or free and `unsafe extern "C"` semantically.
     fn check_c_varadic_type(&self, fk: FnKind<'a>) {
@@ -592,7 +611,7 @@ impl<'a> AstValidator<'a> {
             self.session,
             ident.span,
             E0754,
-            "trying to load file for module `{}` with non ascii identifer name",
+            "trying to load file for module `{}` with non-ascii identifier name",
             ident.name
         )
         .help("consider using `#[path]` attribute to specify filesystem path")
@@ -1103,15 +1122,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
                 self.check_defaultness(fi.span, *def);
                 self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
                 self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
+                self.check_foreign_item_ascii_only(fi.ident);
             }
             ForeignItemKind::TyAlias(box TyAliasKind(def, generics, bounds, body)) => {
                 self.check_defaultness(fi.span, *def);
                 self.check_foreign_kind_bodyless(fi.ident, "type", body.as_ref().map(|b| b.span));
                 self.check_type_no_bounds(bounds, "`extern` blocks");
                 self.check_foreign_ty_genericless(generics);
+                self.check_foreign_item_ascii_only(fi.ident);
             }
             ForeignItemKind::Static(_, _, body) => {
                 self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
+                self.check_foreign_item_ascii_only(fi.ident);
             }
             ForeignItemKind::MacCall(..) => {}
         }
diff --git a/src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs b/src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs
index 5cc04ad5cdf..524ad3c83fc 100644
--- a/src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs
+++ b/src/test/ui/feature-gates/feature-gate-non_ascii_idents.rs
@@ -28,6 +28,7 @@ enum Bär { //~ ERROR non-ascii idents
 
 extern "C" {
     fn qüx();  //~ ERROR non-ascii idents
+    //~^ ERROR items in `extern` blocks
 }
 
 fn main() {}
diff --git a/src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr b/src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr
index e9392ace4ce..c712acee37f 100644
--- a/src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr
+++ b/src/test/ui/feature-gates/feature-gate-non_ascii_idents.stderr
@@ -1,3 +1,13 @@
+error: items in `extern` blocks cannot use non-ascii identifiers
+  --> $DIR/feature-gate-non_ascii_idents.rs:30:8
+   |
+LL | extern "C" {
+   | ---------- in this `extern` block
+LL |     fn qüx();
+   |        ^^^
+   |
+   = note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
+
 error[E0658]: non-ascii idents are not fully supported
   --> $DIR/feature-gate-non_ascii_idents.rs:1:22
    |
@@ -115,6 +125,6 @@ LL |     fn qüx();
    = note: see issue #55467 <https://github.com/rust-lang/rust/issues/55467> for more information
    = help: add `#![feature(non_ascii_idents)]` to the crate attributes to enable
 
-error: aborting due to 13 previous errors
+error: aborting due to 14 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
diff --git a/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.rs b/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.rs
new file mode 100644
index 00000000000..c70ced731d5
--- /dev/null
+++ b/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.rs
@@ -0,0 +1,10 @@
+#![feature(extern_types)]
+#![feature(non_ascii_idents)]
+
+extern "C" {
+    type 一; //~ items in `extern` blocks cannot use non-ascii identifiers
+    fn 二(); //~ items in `extern` blocks cannot use non-ascii identifiers
+    static 三: usize; //~ items in `extern` blocks cannot use non-ascii identifiers
+}
+
+fn main() {}
diff --git a/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.stderr b/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.stderr
new file mode 100644
index 00000000000..3b18c06ec5c
--- /dev/null
+++ b/src/test/ui/rfc-2457/extern_block_nonascii_forbidden.stderr
@@ -0,0 +1,34 @@
+error: items in `extern` blocks cannot use non-ascii identifiers
+  --> $DIR/extern_block_nonascii_forbidden.rs:5:10
+   |
+LL | extern "C" {
+   | ---------- in this `extern` block
+LL |     type 一;
+   |          ^^
+   |
+   = note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
+
+error: items in `extern` blocks cannot use non-ascii identifiers
+  --> $DIR/extern_block_nonascii_forbidden.rs:6:8
+   |
+LL | extern "C" {
+   | ---------- in this `extern` block
+LL |     type 一;
+LL |     fn 二();
+   |        ^^
+   |
+   = note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
+
+error: items in `extern` blocks cannot use non-ascii identifiers
+  --> $DIR/extern_block_nonascii_forbidden.rs:7:12
+   |
+LL | extern "C" {
+   | ---------- in this `extern` block
+...
+LL |     static 三: usize;
+   |            ^^
+   |
+   = note: This limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
+
+error: aborting due to 3 previous errors
+
diff --git a/src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr b/src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr
index be729836f4f..6e06ab737c2 100644
--- a/src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr
+++ b/src/test/ui/rfc-2457/mod_file_nonascii_forbidden.stderr
@@ -6,7 +6,7 @@ LL | mod řųśť;
    |
    = help: to create the module `řųśť`, create file "$DIR/řųśť.rs"
 
-error[E0754]: trying to load file for module `řųśť` with non ascii identifer name
+error[E0754]: trying to load file for module `řųśť` with non-ascii identifier name
   --> $DIR/mod_file_nonascii_forbidden.rs:3:5
    |
 LL | mod řųśť;