Skip to content

Commit d79f1bd

Browse files
committed
Auto merge of #72295 - RalfJung:rollup-icmhbs7, r=RalfJung
Rollup of 3 pull requests Successful merges: - #72259 (Disallow forbidden usage of non-ascii identifiers.) - #72261 (Break out early on empty span when generate_fn_span) - #72291 (bootstrap: fix typo) Failed merges: r? @ghost
2 parents 34cce58 + 2b3d99d commit d79f1bd

12 files changed

+146
-11
lines changed

src/bootstrap/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, path: &Path, check: bool) {
2323
if !status.success() {
2424
eprintln!(
2525
"Running `{}` failed.\nIf you're running `tidy`, \
26-
try again with `--bless` flag. Or, you just want to format \
26+
try again with `--bless`. Or, if you just want to format \
2727
code, run `./x.py fmt` instead.",
2828
cmd_debug,
2929
);

src/librustc_ast_passes/ast_validation.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,35 @@ impl<'a> AstValidator<'a> {
572572
.emit();
573573
}
574574

575+
fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
576+
if ident.name.as_str().is_ascii() {
577+
return;
578+
}
579+
let head_span = self.session.source_map().guess_head_span(item_span);
580+
struct_span_err!(
581+
self.session,
582+
head_span,
583+
E0754,
584+
"`#[no_mangle]` requires ASCII identifier"
585+
)
586+
.emit();
587+
}
588+
589+
fn check_mod_file_item_asciionly(&self, ident: Ident) {
590+
if ident.name.as_str().is_ascii() {
591+
return;
592+
}
593+
struct_span_err!(
594+
self.session,
595+
ident.span,
596+
E0754,
597+
"trying to load file for module `{}` with non ascii identifer name",
598+
ident.name
599+
)
600+
.help("consider using `#[path]` attribute to specify filesystem path")
601+
.emit();
602+
}
603+
575604
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
576605
if !generics.params.is_empty() {
577606
struct_span_err!(
@@ -866,6 +895,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
866895
self.has_proc_macro_decls = true;
867896
}
868897

898+
if attr::contains_name(&item.attrs, sym::no_mangle) {
899+
self.check_nomangle_item_asciionly(item.ident, item.span);
900+
}
901+
869902
match item.kind {
870903
ItemKind::Impl {
871904
unsafety,
@@ -992,9 +1025,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
9921025
walk_list!(self, visit_attribute, &item.attrs);
9931026
return;
9941027
}
995-
ItemKind::Mod(_) => {
1028+
ItemKind::Mod(Mod { inline, .. }) => {
9961029
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
997-
attr::first_attr_value_str_by_name(&item.attrs, sym::path);
1030+
if !inline && !attr::contains_name(&item.attrs, sym::path) {
1031+
self.check_mod_file_item_asciionly(item.ident);
1032+
}
9981033
}
9991034
ItemKind::Union(ref vdata, _) => {
10001035
if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {

src/librustc_error_codes/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ E0750: include_str!("./error_codes/E0750.md"),
436436
E0751: include_str!("./error_codes/E0751.md"),
437437
E0752: include_str!("./error_codes/E0752.md"),
438438
E0753: include_str!("./error_codes/E0753.md"),
439+
E0754: include_str!("./error_codes/E0754.md"),
439440
;
440441
// E0006, // merged with E0005
441442
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
An non-ascii identifier was used in an invalid context.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0754
6+
# #![feature(non_ascii_idents)]
7+
8+
mod řųśť;
9+
// ^ error!
10+
fn main() {}
11+
```
12+
13+
```compile_fail,E0754
14+
# #![feature(non_ascii_idents)]
15+
16+
#[no_mangle]
17+
fn řųśť() {}
18+
// ^ error!
19+
fn main() {}
20+
```
21+
22+
Non-ascii can be used as module names if it is inline
23+
or a #\[path\] attribute is specified. For example:
24+
25+
```
26+
# #![feature(non_ascii_idents)]
27+
28+
mod řųśť {
29+
const IS_GREAT: bool = true;
30+
}
31+
32+
fn main() {}
33+
```

src/librustc_span/source_map.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -910,14 +910,23 @@ impl SourceMap {
910910

911911
pub fn generate_fn_name_span(&self, span: Span) -> Option<Span> {
912912
let prev_span = self.span_extend_to_prev_str(span, "fn", true);
913-
self.span_to_snippet(prev_span)
914-
.map(|snippet| {
915-
let len = snippet
916-
.find(|c: char| !c.is_alphanumeric() && c != '_')
917-
.expect("no label after fn");
918-
prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32))
919-
})
920-
.ok()
913+
if let Ok(snippet) = self.span_to_snippet(prev_span) {
914+
debug!(
915+
"generate_fn_name_span: span={:?}, prev_span={:?}, snippet={:?}",
916+
span, prev_span, snippet
917+
);
918+
919+
if snippet.is_empty() {
920+
return None;
921+
};
922+
923+
let len = snippet
924+
.find(|c: char| !c.is_alphanumeric() && c != '_')
925+
.expect("no label after fn");
926+
Some(prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32)))
927+
} else {
928+
None
929+
}
921930
}
922931

923932
/// Takes the span of a type parameter in a function signature and try to generate a span for
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub trait Foo {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(non_ascii_idents)]
2+
3+
mod řųśť; //~ trying to load file for
4+
//~^ file not found for
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0583]: file not found for module `řųśť`
2+
--> $DIR/mod_file_nonascii_forbidden.rs:3:1
3+
|
4+
LL | mod řųśť;
5+
| ^^^^^^^^^
6+
|
7+
= help: to create the module `řųśť`, create file "$DIR/řųśť.rs"
8+
9+
error[E0754]: trying to load file for module `řųśť` with non ascii identifer name
10+
--> $DIR/mod_file_nonascii_forbidden.rs:3:5
11+
|
12+
LL | mod řųśť;
13+
| ^^^^
14+
|
15+
= help: consider using `#[path]` attribute to specify filesystem path
16+
17+
error: aborting due to 2 previous errors
18+
19+
Some errors have detailed explanations: E0583, E0754.
20+
For more information about an error, try `rustc --explain E0583`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
#![feature(non_ascii_idents)]
3+
4+
#[path="auxiliary/mod_file_nonascii_with_path_allowed-aux.rs"]
5+
mod řųśť;
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
#![feature(non_ascii_idents)]
3+
4+
mod řųśť {
5+
const IS_GREAT: bool = true;
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(non_ascii_idents)]
2+
3+
#[no_mangle]
4+
pub fn řųśť() {} //~ `#[no_mangle]` requires ASCII identifier
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0754]: `#[no_mangle]` requires ASCII identifier
2+
--> $DIR/no_mangle_nonascii_forbidden.rs:4:1
3+
|
4+
LL | pub fn řųśť() {}
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0754`.

0 commit comments

Comments
 (0)