Added additional comments and minor edits

This commit is contained in:
K 2021-04-07 12:35:39 -04:00
parent e433f55852
commit f51f25ab7d
3 changed files with 64 additions and 46 deletions

View file

@ -1596,7 +1596,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub) .try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
.unwrap_or_else(|| self.fresh_binding(ident, pat.id, pat_src, bindings)); .unwrap_or_else(|| self.fresh_binding(ident, pat.id, pat_src, bindings));
self.r.record_partial_res(pat.id, PartialRes::new(res)); self.r.record_partial_res(pat.id, PartialRes::new(res));
self.r.record_local_span(pat.id, pat.span); self.r.record_pat_span(pat.id, pat.span);
} }
PatKind::TupleStruct(ref path, ref sub_patterns) => { PatKind::TupleStruct(ref path, ref sub_patterns) => {
self.smart_resolve_path( self.smart_resolve_path(

View file

@ -884,9 +884,9 @@ pub struct Resolver<'a> {
/// "self-confirming" import resolutions during import validation. /// "self-confirming" import resolutions during import validation.
unusable_binding: Option<&'a NameBinding<'a>>, unusable_binding: Option<&'a NameBinding<'a>>,
// Spans for local variables found during resolution // Spans for local variables found during pattern resolution.
// Used for suggestions during error reporting // Used for suggestions during error reporting.
local_span_map: NodeMap<Span>, pat_span_map: NodeMap<Span>,
/// Resolutions for nodes that have a single resolution. /// Resolutions for nodes that have a single resolution.
partial_res_map: NodeMap<PartialRes>, partial_res_map: NodeMap<PartialRes>,
@ -1266,7 +1266,7 @@ impl<'a> Resolver<'a> {
last_import_segment: false, last_import_segment: false,
unusable_binding: None, unusable_binding: None,
local_span_map: Default::default(), pat_span_map: Default::default(),
partial_res_map: Default::default(), partial_res_map: Default::default(),
import_res_map: Default::default(), import_res_map: Default::default(),
label_res_map: Default::default(), label_res_map: Default::default(),
@ -1884,6 +1884,7 @@ impl<'a> Resolver<'a> {
ribs, ribs,
))); )));
} }
module = match ribs[i].kind { module = match ribs[i].kind {
ModuleRibKind(module) => module, ModuleRibKind(module) => module,
MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => { MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
@ -1894,6 +1895,7 @@ impl<'a> Resolver<'a> {
} }
_ => continue, _ => continue,
}; };
match module.kind { match module.kind {
ModuleKind::Block(..) => {} // We can see through blocks ModuleKind::Block(..) => {} // We can see through blocks
_ => break, _ => break,
@ -1912,8 +1914,7 @@ impl<'a> Resolver<'a> {
return Some(LexicalScopeBinding::Item(binding)); return Some(LexicalScopeBinding::Item(binding));
} }
} }
let returned_item = self self.early_resolve_ident_in_lexical_scope(
.early_resolve_ident_in_lexical_scope(
orig_ident, orig_ident,
ScopeSet::Late(ns, module, record_used_id), ScopeSet::Late(ns, module, record_used_id),
parent_scope, parent_scope,
@ -1922,9 +1923,7 @@ impl<'a> Resolver<'a> {
path_span, path_span,
) )
.ok() .ok()
.map(LexicalScopeBinding::Item); .map(LexicalScopeBinding::Item)
returned_item
} }
fn hygienic_lexical_parent( fn hygienic_lexical_parent(
@ -2391,11 +2390,9 @@ impl<'a> Resolver<'a> {
.next() .next()
.map_or(false, |c| c.is_ascii_uppercase()) .map_or(false, |c| c.is_ascii_uppercase())
{ {
// Add check case for similarly named item in alternative namespace // Check whether the name refers to an item in the value namespace.
let mut suggestion = None; let suggestion = if ribs.is_some() {
let match_span = match self.resolve_ident_in_lexical_scope(
if ribs.is_some() {
if let Some(res) = self.resolve_ident_in_lexical_scope(
ident, ident,
ValueNS, ValueNS,
parent_scope, parent_scope,
@ -2403,26 +2400,47 @@ impl<'a> Resolver<'a> {
path_span, path_span,
&ribs.unwrap()[ValueNS], &ribs.unwrap()[ValueNS],
) { ) {
let mut match_span: Option<Span> = None; // Name matches a local variable. For example:
match res { // ```
LexicalScopeBinding::Res(Res::Local(id)) => { // fn f() {
match_span = // let Foo: &str = "";
Some(*self.local_span_map.get(&id).unwrap()); // println!("{}", Foo::Bar); // Name refers to local
// // variable `Foo`.
// }
// ```
Some(LexicalScopeBinding::Res(Res::Local(id))) => {
Some(*self.pat_span_map.get(&id).unwrap())
} }
LexicalScopeBinding::Item(name_binding) => {
match_span = Some(name_binding.span); // Name matches item from a local name binding
// created by `use` declaration. For example:
// ```
// pub Foo: &str = "";
//
// mod submod {
// use super::Foo;
// println!("{}", Foo::Bar); // Name refers to local
// // binding `Foo`.
// }
// ```
Some(LexicalScopeBinding::Item(name_binding)) => {
Some(name_binding.span)
} }
_ => (), _ => None,
}; };
if let Some(span) = match_span { if let Some(span) = match_span {
suggestion = Some(( Some((
vec![(span, String::from(""))], vec![(span, String::from(""))],
format!("{} is defined here, but is not a type", ident), format!("`{}` is defined here, but is not a type", ident),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
)); ))
} } else {
} None
} }
} else {
None
};
(format!("use of undeclared type `{}`", ident), suggestion) (format!("use of undeclared type `{}`", ident), suggestion)
} else { } else {
@ -2835,9 +2853,9 @@ impl<'a> Resolver<'a> {
} }
} }
fn record_local_span(&mut self, node: NodeId, span: Span) { fn record_pat_span(&mut self, node: NodeId, span: Span) {
debug!("(recording local) recording {:?} for {:?}", node, span); debug!("(recording pat) recording {:?} for {:?}", node, span);
self.local_span_map.insert(node, span); self.pat_span_map.insert(node, span);
} }
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool { fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {

View file

@ -2,7 +2,7 @@ error[E0433]: failed to resolve: use of undeclared type `Baz`
--> $DIR/issue-81508.rs:11:20 --> $DIR/issue-81508.rs:11:20
| |
LL | let Baz: &str = ""; LL | let Baz: &str = "";
| --- help: Baz is defined here, but is not a type | --- help: `Baz` is defined here, but is not a type
LL | LL |
LL | println!("{}", Baz::Bar); LL | println!("{}", Baz::Bar);
| ^^^ use of undeclared type `Baz` | ^^^ use of undeclared type `Baz`
@ -11,7 +11,7 @@ error[E0433]: failed to resolve: use of undeclared type `Foo`
--> $DIR/issue-81508.rs:20:24 --> $DIR/issue-81508.rs:20:24
| |
LL | use super::Foo; LL | use super::Foo;
| ---------- help: Foo is defined here, but is not a type | ---------- help: `Foo` is defined here, but is not a type
LL | fn function() { LL | fn function() {
LL | println!("{}", Foo::Bar); LL | println!("{}", Foo::Bar);
| ^^^ use of undeclared type `Foo` | ^^^ use of undeclared type `Foo`