Skip to content

Commit 944d852

Browse files
committed
Simplify error reporting.
1 parent d1c1bbe commit 944d852

File tree

2 files changed

+76
-85
lines changed

2 files changed

+76
-85
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+76-84
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,12 @@ impl<'a> Resolver<'a> {
13851385
path: &[Segment],
13861386
opt_ns: Option<Namespace>, // `None` indicates a module path in import
13871387
parent_scope: &ParentScope<'a>,
1388-
finalize_full: Finalize,
13891388
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
13901389
unusable_binding: Option<&'a NameBinding<'a>>,
13911390
module: Option<ModuleOrUniformRoot<'a>>,
13921391
i: usize,
13931392
ident: Ident,
13941393
) -> (String, Option<Suggestion>) {
1395-
let finalize = finalize_full.path_span();
13961394
let is_last = i == path.len() - 1;
13971395
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
13981396
let module_res = match module {
@@ -1418,81 +1416,7 @@ impl<'a> Resolver<'a> {
14181416
} else {
14191417
(format!("could not find `{}` in the crate root", ident), None)
14201418
}
1421-
} else if i == 0 {
1422-
if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
1423-
// Check whether the name refers to an item in the value namespace.
1424-
let suggestion = if ribs.is_some() {
1425-
let match_span = match self.resolve_ident_in_lexical_scope(
1426-
ident,
1427-
ValueNS,
1428-
parent_scope,
1429-
Finalize::No,
1430-
&ribs.unwrap()[ValueNS],
1431-
unusable_binding,
1432-
) {
1433-
// Name matches a local variable. For example:
1434-
// ```
1435-
// fn f() {
1436-
// let Foo: &str = "";
1437-
// println!("{}", Foo::Bar); // Name refers to local
1438-
// // variable `Foo`.
1439-
// }
1440-
// ```
1441-
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
1442-
Some(*self.pat_span_map.get(&id).unwrap())
1443-
}
1444-
1445-
// Name matches item from a local name binding
1446-
// created by `use` declaration. For example:
1447-
// ```
1448-
// pub Foo: &str = "";
1449-
//
1450-
// mod submod {
1451-
// use super::Foo;
1452-
// println!("{}", Foo::Bar); // Name refers to local
1453-
// // binding `Foo`.
1454-
// }
1455-
// ```
1456-
Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
1457-
_ => None,
1458-
};
1459-
1460-
if let Some(span) = match_span {
1461-
Some((
1462-
vec![(span, String::from(""))],
1463-
format!("`{}` is defined here, but is not a type", ident),
1464-
Applicability::MaybeIncorrect,
1465-
))
1466-
} else {
1467-
None
1468-
}
1469-
} else {
1470-
None
1471-
};
1472-
1473-
(format!("use of undeclared type `{}`", ident), suggestion)
1474-
} else {
1475-
(
1476-
format!("use of undeclared crate or module `{}`", ident),
1477-
if ident.name == sym::alloc {
1478-
Some((
1479-
vec![],
1480-
String::from("add `extern crate alloc` to use the `alloc` crate"),
1481-
Applicability::MaybeIncorrect,
1482-
))
1483-
} else {
1484-
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module)
1485-
.map(|sugg| {
1486-
(
1487-
vec![(ident.span, sugg.to_string())],
1488-
String::from("there is a crate or module with a similar name"),
1489-
Applicability::MaybeIncorrect,
1490-
)
1491-
})
1492-
},
1493-
)
1494-
}
1495-
} else {
1419+
} else if i > 0 {
14961420
let parent = path[i - 1].ident.name;
14971421
let parent = match parent {
14981422
// ::foo is mounted at the crate root for 2015, and is the extern
@@ -1501,9 +1425,7 @@ impl<'a> Resolver<'a> {
15011425
"the list of imported crates".to_owned()
15021426
}
15031427
kw::PathRoot | kw::Crate => "the crate root".to_owned(),
1504-
_ => {
1505-
format!("`{}`", parent)
1506-
}
1428+
_ => format!("`{}`", parent),
15071429
};
15081430

15091431
let mut msg = format!("could not find `{}` in {}", ident, parent);
@@ -1515,7 +1437,7 @@ impl<'a> Resolver<'a> {
15151437
ident,
15161438
ns_to_try,
15171439
parent_scope,
1518-
finalize,
1440+
None,
15191441
false,
15201442
unusable_binding,
15211443
).ok()
@@ -1526,7 +1448,7 @@ impl<'a> Resolver<'a> {
15261448
ident,
15271449
ns_to_try,
15281450
parent_scope,
1529-
finalize_full,
1451+
Finalize::No,
15301452
&ribs[ns_to_try],
15311453
unusable_binding,
15321454
) {
@@ -1540,8 +1462,8 @@ impl<'a> Resolver<'a> {
15401462
ident,
15411463
scopes,
15421464
parent_scope,
1543-
finalize,
1544-
finalize.is_some(),
1465+
None,
1466+
false,
15451467
false,
15461468
unusable_binding,
15471469
).ok()
@@ -1567,6 +1489,76 @@ impl<'a> Resolver<'a> {
15671489
};
15681490
}
15691491
(msg, None)
1492+
} else if ident.name.as_str().chars().next().map_or(false, |c| c.is_ascii_uppercase()) {
1493+
// Check whether the name refers to an item in the value namespace.
1494+
let binding = if let Some(ribs) = ribs {
1495+
self.resolve_ident_in_lexical_scope(
1496+
ident,
1497+
ValueNS,
1498+
parent_scope,
1499+
Finalize::No,
1500+
&ribs[ValueNS],
1501+
unusable_binding,
1502+
)
1503+
} else {
1504+
None
1505+
};
1506+
let match_span = match binding {
1507+
// Name matches a local variable. For example:
1508+
// ```
1509+
// fn f() {
1510+
// let Foo: &str = "";
1511+
// println!("{}", Foo::Bar); // Name refers to local
1512+
// // variable `Foo`.
1513+
// }
1514+
// ```
1515+
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
1516+
Some(*self.pat_span_map.get(&id).unwrap())
1517+
}
1518+
// Name matches item from a local name binding
1519+
// created by `use` declaration. For example:
1520+
// ```
1521+
// pub Foo: &str = "";
1522+
//
1523+
// mod submod {
1524+
// use super::Foo;
1525+
// println!("{}", Foo::Bar); // Name refers to local
1526+
// // binding `Foo`.
1527+
// }
1528+
// ```
1529+
Some(LexicalScopeBinding::Item(name_binding)) => Some(name_binding.span),
1530+
_ => None,
1531+
};
1532+
let suggestion = if let Some(span) = match_span {
1533+
Some((
1534+
vec![(span, String::from(""))],
1535+
format!("`{}` is defined here, but is not a type", ident),
1536+
Applicability::MaybeIncorrect,
1537+
))
1538+
} else {
1539+
None
1540+
};
1541+
1542+
(format!("use of undeclared type `{}`", ident), suggestion)
1543+
} else {
1544+
let suggestion = if ident.name == sym::alloc {
1545+
Some((
1546+
vec![],
1547+
String::from("add `extern crate alloc` to use the `alloc` crate"),
1548+
Applicability::MaybeIncorrect,
1549+
))
1550+
} else {
1551+
self.find_similarly_named_module_or_crate(ident.name, &parent_scope.module).map(
1552+
|sugg| {
1553+
(
1554+
vec![(ident.span, sugg.to_string())],
1555+
String::from("there is a crate or module with a similar name"),
1556+
Applicability::MaybeIncorrect,
1557+
)
1558+
},
1559+
)
1560+
};
1561+
(format!("use of undeclared crate or module `{}`", ident), suggestion)
15701562
}
15711563
}
15721564
}

compiler/rustc_resolve/src/ident.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,6 @@ impl<'a> Resolver<'a> {
15681568
path,
15691569
opt_ns,
15701570
parent_scope,
1571-
finalize_full,
15721571
ribs,
15731572
unusable_binding,
15741573
module,

0 commit comments

Comments
 (0)