Skip to content

Commit 22ac606

Browse files
committed
fix rust-lang#101749, use . instead of :: when accessing a method of an object
1 parent 126dbdc commit 22ac606

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,13 +1994,20 @@ impl<'a> Resolver<'a> {
19941994

19951995
(format!("use of undeclared type `{}`", ident), suggestion)
19961996
} else {
1997-
let suggestion = if ident.name == sym::alloc {
1998-
Some((
1997+
// crate or module resolve failed, here we try three things:
1998+
1999+
// 1. special handling for `alloc`
2000+
let mut suggestion = None;
2001+
if ident.name == sym::alloc {
2002+
suggestion = Some((
19992003
vec![],
20002004
String::from("add `extern crate alloc` to use the `alloc` crate"),
20012005
Applicability::MaybeIncorrect,
20022006
))
2003-
} else {
2007+
}
2008+
2009+
// 2. check whether there is a similar name
2010+
suggestion = suggestion.or_else(|| {
20042011
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
20052012
|sugg| {
20062013
(
@@ -2010,7 +2017,39 @@ impl<'a> Resolver<'a> {
20102017
)
20112018
},
20122019
)
2013-
};
2020+
});
2021+
2022+
// 3. check whether the name refers to an item in local scope
2023+
if suggestion.is_none() &&
2024+
let Some(ribs) = ribs &&
2025+
let Some(LexicalScopeBinding::Res(Res::Local(_))) = self.resolve_ident_in_lexical_scope(
2026+
ident,
2027+
ValueNS,
2028+
parent_scope,
2029+
None,
2030+
&ribs[ValueNS],
2031+
ignore_binding,
2032+
)
2033+
{
2034+
let sm = self.session.source_map();
2035+
let code_span = sm
2036+
.span_extend_while(ident.span.shrink_to_hi(), |c| c != '\n')
2037+
.unwrap_or(ident.span);
2038+
let code = sm.span_to_snippet(code_span).unwrap();
2039+
if code.starts_with("::") && code.matches("::").count() == 1 {
2040+
suggestion = Some((
2041+
vec![(
2042+
sm.span_extend_while(ident.span, |c| c == ':').unwrap(),
2043+
format!("{}.", ident),
2044+
)],
2045+
format!(
2046+
"`{}` is not a crate or module, maybe you meant to call instance method",
2047+
ident
2048+
),
2049+
Applicability::MaybeIncorrect,
2050+
))
2051+
}
2052+
};
20142053
(format!("use of undeclared crate or module `{}`", ident), suggestion)
20152054
}
20162055
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
struct Rectangle {
3+
width: i32,
4+
height: i32,
5+
}
6+
impl Rectangle {
7+
fn new(width: i32, height: i32) -> Self {
8+
Self { width, height }
9+
}
10+
fn area(&self) -> i32 {
11+
self.height * self.width
12+
}
13+
}
14+
15+
fn main() {
16+
let width = 3;
17+
let height = 4;
18+
let rect1 = Rectangle::new(width, height);
19+
println!("{}", rect1.area());
20+
//~^ ERROR failed to resolve: use of undeclared crate or module `rect1`
21+
}

src/test/ui/resolve/issue-101749.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
struct Rectangle {
3+
width: i32,
4+
height: i32,
5+
}
6+
impl Rectangle {
7+
fn new(width: i32, height: i32) -> Self {
8+
Self { width, height }
9+
}
10+
fn area(&self) -> i32 {
11+
self.height * self.width
12+
}
13+
}
14+
15+
fn main() {
16+
let width = 3;
17+
let height = 4;
18+
let rect1 = Rectangle::new(width, height);
19+
println!("{}", rect1::area());
20+
//~^ ERROR failed to resolve: use of undeclared crate or module `rect1`
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0433]: failed to resolve: use of undeclared crate or module `rect1`
2+
--> $DIR/issue-101749.rs:19:20
3+
|
4+
LL | println!("{}", rect1::area());
5+
| ^^^^^ use of undeclared crate or module `rect1`
6+
|
7+
help: `rect1` is not a crate or module, maybe you meant to call instance method
8+
|
9+
LL | println!("{}", rect1.area());
10+
| ~~~~~~
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)