Skip to content

Commit 5a2c301

Browse files
committed
Do not complain about missing crate named as a keyword
1 parent 5918318 commit 5a2c301

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

src/librustc_resolve/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,8 @@ enum PathResult<'a> {
10341034
NonModule(PathResolution),
10351035
Indeterminate,
10361036
Failed(Span, String, bool /* is the error from the last segment? */),
1037+
/// Encountered an error that is reported elsewhere
1038+
Ignore,
10371039
}
10381040

10391041
enum ModuleKind {
@@ -1766,6 +1768,7 @@ impl<'a> Resolver<'a> {
17661768
error_callback(self, span, ResolutionError::FailedToResolve(&msg));
17671769
Def::Err
17681770
}
1771+
PathResult::Ignore => Def::Err,
17691772
};
17701773

17711774
let segments: Vec<_> = segments.iter().map(|seg| {
@@ -3693,7 +3696,7 @@ impl<'a> Resolver<'a> {
36933696
resolve_error(self, span, ResolutionError::FailedToResolve(&msg));
36943697
err_path_resolution()
36953698
}
3696-
PathResult::Module(..) | PathResult::Failed(..) => return None,
3699+
PathResult::Module(..) | PathResult::Failed(..) | PathResult::Ignore => return None,
36973700
PathResult::Indeterminate => bug!("indetermined path result in resolve_qpath"),
36983701
};
36993702

@@ -3925,8 +3928,11 @@ impl<'a> Resolver<'a> {
39253928
});
39263929
if let Some(candidate) = candidates.get(0) {
39273930
format!("did you mean `{}`?", candidate.path)
3928-
} else {
3931+
} else if !ident.is_used_keyword() {
39293932
format!("maybe a missing `extern crate {};`?", ident)
3933+
} else {
3934+
// the parser will already have complained about the keyword being used
3935+
return PathResult::Ignore;
39303936
}
39313937
} else if i == 0 {
39323938
format!("use of undeclared type or module `{}`", ident)

src/librustc_resolve/macros.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ impl<'a> Resolver<'a> {
364364
Ok(path_res.base_def())
365365
}
366366
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
367-
PathResult::NonModule(..) | PathResult::Indeterminate | PathResult::Failed(..) => {
367+
PathResult::NonModule(..) | PathResult::Indeterminate |
368+
PathResult::Failed(..) | PathResult::Ignore => {
368369
Err(Determinacy::Determined)
369370
}
370371
PathResult::Module(..) => unreachable!(),
@@ -929,7 +930,8 @@ impl<'a> Resolver<'a> {
929930
let def = path_res.base_def();
930931
check_consistency(self, &path, path_span, kind, initial_def, def);
931932
}
932-
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed(..) => {
933+
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed(..) |
934+
path_res @ PathResult::Ignore => {
933935
let (span, msg) = if let PathResult::Failed(span, msg, ..) = path_res {
934936
(span, msg)
935937
} else {

src/librustc_resolve/resolve_imports.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
767767
match path_res {
768768
PathResult::Module(module) => module,
769769
PathResult::Indeterminate => return false,
770-
PathResult::NonModule(..) | PathResult::Failed(..) => return true,
770+
PathResult::NonModule(..) | PathResult::Failed(..) |
771+
PathResult::Ignore => return true,
771772
}
772773
};
773774

@@ -861,6 +862,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
861862

862863
module
863864
}
865+
PathResult::Ignore => {
866+
return None;
867+
}
864868
PathResult::Failed(span, msg, false) => {
865869
if no_ambiguity {
866870
assert!(directive.imported_module.get().is_none());
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-pass
2+
3+
mod m {
4+
pub fn r#for() {}
5+
}
6+
7+
fn main() {
8+
m::r#for();
9+
}

src/test/ui/issues/issue-57198.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod m {
2+
pub fn r#for() {}
3+
}
4+
5+
fn main() {
6+
m::for();
7+
//~^ ERROR expected identifier, found keyword `for`
8+
}

src/test/ui/issues/issue-57198.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected identifier, found keyword `for`
2+
--> $DIR/issue-57198.rs:6:8
3+
|
4+
LL | m::for();
5+
| ^^^ expected identifier, found keyword
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)