Skip to content

Commit f7e83db

Browse files
committed
fix rust-lang#101749, use . instead of :: when accessing a method of an object
1 parent 224a190 commit f7e83db

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,21 +2004,28 @@ impl<'a> Resolver<'a> {
20042004
&ribs[ValueNS],
20052005
ignore_binding,
20062006
);
2007-
println!("res: {:?}", res);
2007+
/* println!("res: {:?}", res);
2008+
println!("span: {:?}", ident.span); */
20082009
let bind = match res {
20092010
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
20102011
Some(*self.pat_span_map.get(&id).unwrap())
20112012
}
20122013
_ => None,
20132014
};
2014-
let next = self.next_node_id();
2015+
/* let next = self.next_node_id();
20152016
println!("bind: {:?}", bind);
2016-
println!("next: {:?}", next);
2017-
if bind.is_some() {
2017+
println!("next: {:?}", next); */
2018+
let sm = self.session.source_map();
2019+
let expand_span =
2020+
sm.span_extend_while(ident.span, |c| c == ':').unwrap_or(ident.span);
2021+
//println!("expand_span: {:?}", expand_span);
2022+
let diff = expand_span.hi() - ident.span.hi();
2023+
if let Some(_) = bind && diff == rustc_span::BytePos(2)
2024+
{
20182025
Some((
2019-
vec![(bind.unwrap(), String::from(""))],
2026+
vec![(expand_span, format!("{}.", ident))],
20202027
format!(
2021-
"`{}` is defined here, but is not a crate or module",
2028+
"`{}` is not a crate or module, maybe you meant to call instance method",
20222029
ident
20232030
),
20242031
Applicability::MaybeIncorrect,
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
21+
}
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
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)