From 0749ec67bc927eba90f4be5544055d3cd9040e58 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 23 Mar 2022 22:47:19 +0300 Subject: [PATCH 1/4] resolve: Do not build expensive suggestions if they are not actually used Also remove a redundant parameter from `fn resolve_path(_with_ribs)`, `crate_lint: CrateLint` is a more detailed version of `record_used: bool` with `CrateLint::No` meaning `false` and anything else meaning `true`. --- .../rustc_resolve/src/build_reduced_graph.rs | 3 +- compiler/rustc_resolve/src/diagnostics.rs | 8 +- compiler/rustc_resolve/src/imports.rs | 16 +- compiler/rustc_resolve/src/late.rs | 30 +- .../rustc_resolve/src/late/diagnostics.rs | 6 +- compiler/rustc_resolve/src/lib.rs | 401 +++++++++--------- compiler/rustc_resolve/src/macros.rs | 6 +- .../edition-lint-nested-empty-paths.fixed | 10 - .../edition-lint-nested-empty-paths.rs | 10 - .../edition-lint-nested-empty-paths.stderr | 55 +-- .../rust-2018/edition-lint-nested-paths.fixed | 6 - .../ui/rust-2018/edition-lint-nested-paths.rs | 6 - .../edition-lint-nested-paths.stderr | 33 +- .../ui/rust-2018/edition-lint-paths.fixed | 6 - src/test/ui/rust-2018/edition-lint-paths.rs | 6 - .../ui/rust-2018/edition-lint-paths.stderr | 45 +- .../ui/rust-2018/extern-crate-rename.fixed | 2 - src/test/ui/rust-2018/extern-crate-rename.rs | 2 - .../ui/rust-2018/extern-crate-rename.stderr | 11 +- .../ui/rust-2018/extern-crate-submod.fixed | 3 - src/test/ui/rust-2018/extern-crate-submod.rs | 3 - .../ui/rust-2018/extern-crate-submod.stderr | 11 +- 22 files changed, 234 insertions(+), 445 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 2c3ddae9cb4bb..924e2d43785de 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -296,9 +296,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { &segments, Some(TypeNS), parent_scope, - !speculative, path.span, - CrateLint::SimplePath(id), + if speculative { CrateLint::No } else { CrateLint::SimplePath(id) }, ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => { let res = module.res().expect("visibility resolved to unnamed block"); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e88b6a57376c3..e678ac9d518f5 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1426,7 +1426,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `self` and check if that is valid. path[0].ident.name = kw::SelfLower; - let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1446,7 +1446,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Crate; - let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some(( @@ -1478,7 +1478,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Super; - let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1513,7 +1513,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { for name in extern_crate_names.into_iter() { // Replace first ident with a crate name and check if that is valid. path[0].ident.name = name; - let result = self.r.resolve_path(&path, None, parent_scope, false, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); debug!( "make_external_crate_suggestion: name={:?} path={:?} result={:?}", name, path, result diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 70ade7a5600ba..87c06559ccaa7 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -123,10 +123,6 @@ impl<'a> Import<'a> { _ => false, } } - - crate fn crate_lint(&self) -> CrateLint { - CrateLint::UsePath { root_id: self.root_id, root_span: self.root_span } - } } #[derive(Clone, Default, Debug)] @@ -791,9 +787,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> { &import.module_path, None, &import.parent_scope, - false, import.span, - import.crate_lint(), + CrateLint::No, ); import.vis.set(orig_vis); @@ -887,13 +882,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> { _ => None, }; let prev_ambiguity_errors_len = self.r.ambiguity_errors.len(); + let crate_lint = + CrateLint::UsePath { root_id: import.root_id, root_span: import.root_span }; let path_res = self.r.resolve_path( &import.module_path, None, &import.parent_scope, - true, import.span, - import.crate_lint(), + crate_lint, ); let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len; if let Some(orig_unusable_binding) = orig_unusable_binding { @@ -982,7 +978,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { let mut full_path = import.module_path.clone(); full_path.push(Segment::from_ident(Ident::empty())); self.r.lint_if_path_starts_with_module( - import.crate_lint(), + crate_lint, &full_path, import.span, None, @@ -1254,7 +1250,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { self.r.per_ns(|this, ns| { if let Ok(binding) = source_bindings[ns].get() { this.lint_if_path_starts_with_module( - import.crate_lint(), + crate_lint, &full_path, import.span, Some(binding), diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index d5b1aa00e52de..511dc285cb4f4 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -768,7 +768,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { &mut self, path: &[Segment], opt_ns: Option, // `None` indicates a module path in import - record_used: bool, path_span: Span, crate_lint: CrateLint, ) -> PathResult<'a> { @@ -776,7 +775,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { path, opt_ns, &self.parent_scope, - record_used, path_span, crate_lint, Some(&self.ribs), @@ -1253,18 +1251,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { PathSource::Trait(AliasPossibility::No), CrateLint::SimplePath(trait_ref.ref_id), ); - let res = res.base_res(); - if res != Res::Err { - if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = self.resolve_path( - &path, - Some(TypeNS), - true, - trait_ref.path.span, - CrateLint::SimplePath(trait_ref.ref_id), - ) { - new_id = Some(res.def_id()); - new_val = Some((module, trait_ref.clone())); - } + if let Some(def_id) = res.base_res().opt_def_id() { + new_id = Some(def_id); + new_val = Some((self.r.expect_module(def_id), trait_ref.clone())); } } let original_trait_ref = replace(&mut self.current_trait_ref, new_val); @@ -2026,6 +2015,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { None }; + assert_ne!(crate_lint, CrateLint::No); let partial_res = match self.resolve_qpath_anywhere( id, qself, @@ -2060,7 +2050,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std))); std_path.extend(path); if let PathResult::Module(_) | PathResult::NonModule(_) = - self.resolve_path(&std_path, Some(ns), false, span, CrateLint::No) + self.resolve_path(&std_path, Some(ns), span, CrateLint::No) { // Check if we wrote `str::from_utf8` instead of `std::str::from_utf8` let item_span = @@ -2228,7 +2218,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ))); } - let result = match self.resolve_path(&path, Some(ns), true, span, crate_lint) { + let result = match self.resolve_path(&path, Some(ns), span, crate_lint) { PathResult::NonModule(path_res) => path_res, PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => { PartialRes::new(module.res().unwrap()) @@ -2268,13 +2258,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { && path[0].ident.name != kw::DollarCrate { let unqualified_result = { - match self.resolve_path( - &[*path.last().unwrap()], - Some(ns), - false, - span, - CrateLint::No, - ) { + match self.resolve_path(&[*path.last().unwrap()], Some(ns), span, CrateLint::No) { PathResult::NonModule(path_res) => path_res.base_res(), PathResult::Module(ModuleOrUniformRoot::Module(module)) => { module.res().unwrap() diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 1394f4083d0b9..3c6caa7953771 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -188,7 +188,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } else { let mod_path = &path[..path.len() - 1]; let mod_prefix = - match self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No) { + match self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(), _ => None, } @@ -648,7 +648,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { if let crate::PathSource::TraitItem(_) = source { let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, None, false, span, CrateLint::No) + self.resolve_path(mod_path, None, span, CrateLint::No) { let resolutions = self.r.resolutions(module).borrow(); let targets: Vec<_> = @@ -1384,7 +1384,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { // Search in module. let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, Some(TypeNS), false, span, CrateLint::No) + self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No) { self.r.add_module_candidates(module, &mut names, &filter_fn); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 77fd71159381a..5a87dede54e36 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -449,6 +449,19 @@ enum PathResult<'a> { }, } +impl<'a> PathResult<'a> { + fn failed( + span: Span, + is_error_from_last_segment: bool, + record_used: bool, + label_and_suggestion: impl FnOnce() -> (String, Option), + ) -> PathResult<'a> { + let (label, suggestion) = + if record_used { label_and_suggestion() } else { (String::new(), None) }; + PathResult::Failed { span, label, suggestion, is_error_from_last_segment } + } +} + #[derive(Debug)] enum ModuleKind { /// An anonymous module; e.g., just a block. @@ -2206,19 +2219,10 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - record_used: bool, path_span: Span, crate_lint: CrateLint, ) -> PathResult<'a> { - self.resolve_path_with_ribs( - path, - opt_ns, - parent_scope, - record_used, - path_span, - crate_lint, - None, - ) + self.resolve_path_with_ribs(path, opt_ns, parent_scope, path_span, crate_lint, None) } fn resolve_path_with_ribs( @@ -2226,19 +2230,18 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - record_used: bool, path_span: Span, crate_lint: CrateLint, ribs: Option<&PerNS>>>, ) -> PathResult<'a> { + let record_used = crate_lint != CrateLint::No; let mut module = None; let mut allow_super = true; let mut second_binding = None; debug!( - "resolve_path(path={:?}, opt_ns={:?}, record_used={:?}, \ - path_span={:?}, crate_lint={:?})", - path, opt_ns, record_used, path_span, crate_lint, + "resolve_path(path={:?}, opt_ns={:?}, path_span={:?}, crate_lint={:?})", + path, opt_ns, path_span, crate_lint, ); for (i, &Segment { ident, id, has_generic_args: _ }) in path.iter().enumerate() { @@ -2278,13 +2281,9 @@ impl<'a> Resolver<'a> { continue; } } - let msg = "there are too many leading `super` keywords".to_string(); - return PathResult::Failed { - span: ident.span, - label: msg, - suggestion: None, - is_error_from_last_segment: false, - }; + return PathResult::failed(ident.span, false, record_used, || { + ("there are too many leading `super` keywords".to_string(), None) + }); } if i == 0 { if name == kw::SelfLower { @@ -2313,22 +2312,19 @@ impl<'a> Resolver<'a> { // Report special messages for path segment keywords in wrong positions. if ident.is_path_segment_keyword() && i != 0 { - let name_str = if name == kw::PathRoot { - "crate root".to_string() - } else { - format!("`{}`", name) - }; - let label = if i == 1 && path[0].ident.name == kw::PathRoot { - format!("global paths cannot start with {}", name_str) - } else { - format!("{} in paths can only be used in start position", name_str) - }; - return PathResult::Failed { - span: ident.span, - label, - suggestion: None, - is_error_from_last_segment: false, - }; + return PathResult::failed(ident.span, false, record_used, || { + let name_str = if name == kw::PathRoot { + "crate root".to_string() + } else { + format!("`{}`", name) + }; + let label = if i == 1 && path[0].ident.name == kw::PathRoot { + format!("global paths cannot start with {}", name_str) + } else { + format!("{} in paths can only be used in start position", name_str) + }; + (label, None) + }); } enum FindBindingResult<'a> { @@ -2356,16 +2352,11 @@ impl<'a> Resolver<'a> { path_span, ) } else { - let record_used_id = if record_used { - crate_lint.node_id().or(Some(CRATE_NODE_ID)) - } else { - None - }; match this.resolve_ident_in_lexical_scope( ident, ns, parent_scope, - record_used_id, + crate_lint.node_id(), path_span, &ribs.unwrap()[ns], ) { @@ -2425,19 +2416,14 @@ impl<'a> Resolver<'a> { path.len() - i - 1, )); } else { - let label = format!( - "`{}` is {} {}, not a module", - ident, - res.article(), - res.descr(), - ); - - return PathResult::Failed { - span: ident.span, - label, - suggestion: None, - is_error_from_last_segment: is_last, - }; + return PathResult::failed(ident.span, is_last, record_used, || { + let label = format!( + "`{ident}` is {} {}, not a module", + res.article(), + res.descr() + ); + (label, None) + }); } } Err(Undetermined) => return PathResult::Indeterminate, @@ -2450,171 +2436,169 @@ impl<'a> Resolver<'a> { )); } } - let module_res = match module { - Some(ModuleOrUniformRoot::Module(module)) => module.res(), - _ => None, - }; - let (label, suggestion) = if module_res == self.graph_root.res() { - let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _)); - // Don't look up import candidates if this is a speculative resolve - let mut candidates = if record_used { - self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod) - } else { - Vec::new() - }; - candidates.sort_by_cached_key(|c| { - (c.path.segments.len(), pprust::path_to_string(&c.path)) - }); - if let Some(candidate) = candidates.get(0) { - ( - String::from("unresolved import"), - Some(( - vec![(ident.span, pprust::path_to_string(&candidate.path))], - String::from("a similar path exists"), - Applicability::MaybeIncorrect, - )), - ) - } else if self.session.edition() == Edition::Edition2015 { - (format!("maybe a missing crate `{}`?", ident), None) - } else { - (format!("could not find `{}` in the crate root", ident), None) - } - } else if i == 0 { - if ident - .name - .as_str() - .chars() - .next() - .map_or(false, |c| c.is_ascii_uppercase()) - { - // Check whether the name refers to an item in the value namespace. - let suggestion = if ribs.is_some() { - let match_span = match self.resolve_ident_in_lexical_scope( - ident, - ValueNS, - parent_scope, - None, - path_span, - &ribs.unwrap()[ValueNS], - ) { - // Name matches a local variable. For example: - // ``` - // fn f() { - // let Foo: &str = ""; - // println!("{}", Foo::Bar); // Name refers to local - // // variable `Foo`. - // } - // ``` - Some(LexicalScopeBinding::Res(Res::Local(id))) => { - Some(*self.pat_span_map.get(&id).unwrap()) - } - // 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 { + return PathResult::failed(ident.span, is_last, record_used, || { + let module_res = match module { + Some(ModuleOrUniformRoot::Module(module)) => module.res(), + _ => None, + }; + if module_res == self.graph_root.res() { + let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _)); + let mut candidates = + self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod); + candidates.sort_by_cached_key(|c| { + (c.path.segments.len(), pprust::path_to_string(&c.path)) + }); + if let Some(candidate) = candidates.get(0) { + ( + String::from("unresolved import"), Some(( - vec![(span, String::from(""))], - format!("`{}` is defined here, but is not a type", ident), + vec![(ident.span, pprust::path_to_string(&candidate.path))], + String::from("a similar path exists"), Applicability::MaybeIncorrect, - )) - } else { - None - } + )), + ) + } else if self.session.edition() == Edition::Edition2015 { + (format!("maybe a missing crate `{}`?", ident), None) } else { - None - }; + (format!("could not find `{}` in the crate root", ident), None) + } + } else if i == 0 { + if ident + .name + .as_str() + .chars() + .next() + .map_or(false, |c| c.is_ascii_uppercase()) + { + // Check whether the name refers to an item in the value namespace. + let suggestion = if ribs.is_some() { + let match_span = match self.resolve_ident_in_lexical_scope( + ident, + ValueNS, + parent_scope, + None, + path_span, + &ribs.unwrap()[ValueNS], + ) { + // Name matches a local variable. For example: + // ``` + // fn f() { + // let Foo: &str = ""; + // println!("{}", Foo::Bar); // Name refers to local + // // variable `Foo`. + // } + // ``` + Some(LexicalScopeBinding::Res(Res::Local(id))) => { + Some(*self.pat_span_map.get(&id).unwrap()) + } + + // 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, + }; - (format!("use of undeclared type `{}`", ident), suggestion) - } else { - ( - format!("use of undeclared crate or module `{}`", ident), - if ident.name == sym::alloc { - Some(( - vec![], - String::from( - "add `extern crate alloc` to use the `alloc` crate", - ), - Applicability::MaybeIncorrect, - )) + if let Some(span) = match_span { + Some(( + vec![(span, String::from(""))], + format!( + "`{}` is defined here, but is not a type", + ident + ), + Applicability::MaybeIncorrect, + )) + } else { + None + } } else { - self.find_similarly_named_module_or_crate( - ident.name, - &parent_scope.module, - ) - .map(|sugg| { - ( - vec![(ident.span, sugg.to_string())], + None + }; + + (format!("use of undeclared type `{}`", ident), suggestion) + } else { + ( + format!("use of undeclared crate or module `{}`", ident), + if ident.name == sym::alloc { + Some(( + vec![], String::from( - "there is a crate or module with a similar name", + "add `extern crate alloc` to use the `alloc` crate", ), Applicability::MaybeIncorrect, + )) + } else { + self.find_similarly_named_module_or_crate( + ident.name, + &parent_scope.module, ) - }) - }, - ) - } - } else { - let parent = path[i - 1].ident.name; - let parent = match parent { - // ::foo is mounted at the crate root for 2015, and is the extern - // prelude for 2018+ - kw::PathRoot if self.session.edition() > Edition::Edition2015 => { - "the list of imported crates".to_owned() - } - kw::PathRoot | kw::Crate => "the crate root".to_owned(), - _ => { - format!("`{}`", parent) + .map(|sugg| { + ( + vec![(ident.span, sugg.to_string())], + String::from( + "there is a crate or module with a similar name", + ), + Applicability::MaybeIncorrect, + ) + }) + }, + ) } - }; - - let mut msg = format!("could not find `{}` in {}", ident, parent); - if ns == TypeNS || ns == ValueNS { - let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS }; - if let FindBindingResult::Binding(Ok(binding)) = - find_binding_in_ns(self, ns_to_try) - { - let mut found = |what| { - msg = format!( - "expected {}, found {} `{}` in {}", - ns.descr(), - what, - ident, - parent - ) - }; - if binding.module().is_some() { - found("module") - } else { - match binding.res() { - def::Res::::Def(kind, id) => found(kind.descr(id)), - _ => found(ns_to_try.descr()), - } + } else { + let parent = path[i - 1].ident.name; + let parent = match parent { + // ::foo is mounted at the crate root for 2015, and is the extern + // prelude for 2018+ + kw::PathRoot if self.session.edition() > Edition::Edition2015 => { + "the list of imported crates".to_owned() + } + kw::PathRoot | kw::Crate => "the crate root".to_owned(), + _ => { + format!("`{}`", parent) } }; + + let mut msg = format!("could not find `{}` in {}", ident, parent); + if ns == TypeNS || ns == ValueNS { + let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS }; + if let FindBindingResult::Binding(Ok(binding)) = + find_binding_in_ns(self, ns_to_try) + { + let mut found = |what| { + msg = format!( + "expected {}, found {} `{}` in {}", + ns.descr(), + what, + ident, + parent + ) + }; + if binding.module().is_some() { + found("module") + } else { + match binding.res() { + def::Res::::Def(kind, id) => { + found(kind.descr(id)) + } + _ => found(ns_to_try.descr()), + } + } + }; + } + (msg, None) } - (msg, None) - }; - return PathResult::Failed { - span: ident.span, - label, - suggestion, - is_error_from_last_segment: is_last, - }; + }); } } } @@ -3374,7 +3358,6 @@ impl<'a> Resolver<'a> { &Segment::from_path(path), Some(ns), parent_scope, - false, path.span, CrateLint::No, ) { @@ -3543,7 +3526,7 @@ fn module_to_string(module: Module<'_>) -> Option { Some(names_to_string(&names)) } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, PartialEq, Debug)] enum CrateLint { /// Do not issue the lint. No, diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index e34d3e605ecdf..d30aed71e23e8 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -415,7 +415,7 @@ impl<'a> ResolverExpand for Resolver<'a> { let mut indeterminate = false; for ns in [TypeNS, ValueNS, MacroNS].iter().copied() { - match self.resolve_path(path, Some(ns), &parent_scope, false, span, CrateLint::No) { + match self.resolve_path(path, Some(ns), &parent_scope, span, CrateLint::No) { PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true), PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => { return Ok(true); @@ -579,7 +579,6 @@ impl<'a> Resolver<'a> { &path, Some(MacroNS), parent_scope, - false, path_span, CrateLint::No, ) { @@ -1033,9 +1032,8 @@ impl<'a> Resolver<'a> { &path, Some(MacroNS), &parent_scope, - true, path_span, - CrateLint::No, + CrateLint::SimplePath(ast::CRATE_NODE_ID), ) { PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { let res = path_res.base_res(); diff --git a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.fixed b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.fixed index acb0aa420abce..5786ed7b1d533 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.fixed +++ b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.fixed @@ -17,28 +17,18 @@ crate mod foo { use crate::foo::{bar::{baz::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition use crate::foo::{bar::{XX, baz::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition //~| ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition use crate::foo::{bar::{baz::{}, baz1::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition //~| ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition fn main() { } diff --git a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.rs b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.rs index 4825528e97fa3..b7c86088c75ed 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.rs +++ b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.rs @@ -17,28 +17,18 @@ crate mod foo { use foo::{bar::{baz::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition use foo::{bar::{XX, baz::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition //~| ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition use foo::{bar::{baz::{}, baz1::{}}}; //~^ ERROR absolute paths must start with //~| WARN this is accepted in the current edition //~| ERROR absolute paths must start with //~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| WARN this is accepted in the current edition fn main() { } diff --git a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.stderr b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.stderr index 8a3113729b469..e47c320f78f09 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-empty-paths.stderr +++ b/src/test/ui/rust-2018/edition-lint-nested-empty-paths.stderr @@ -13,16 +13,7 @@ LL | #![deny(absolute_paths_not_starting_with_crate)] = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:17:5 - | -LL | use foo::{bar::{baz::{}}}; - | ^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{baz::{}}}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:23:5 + --> $DIR/edition-lint-nested-empty-paths.rs:21:5 | LL | use foo::{bar::{XX, baz::{}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{XX, baz::{}}}` @@ -31,7 +22,7 @@ LL | use foo::{bar::{XX, baz::{}}}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:23:5 + --> $DIR/edition-lint-nested-empty-paths.rs:21:5 | LL | use foo::{bar::{XX, baz::{}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{XX, baz::{}}}` @@ -40,43 +31,7 @@ LL | use foo::{bar::{XX, baz::{}}}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:23:5 - | -LL | use foo::{bar::{XX, baz::{}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{XX, baz::{}}}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:23:5 - | -LL | use foo::{bar::{XX, baz::{}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{XX, baz::{}}}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:33:5 - | -LL | use foo::{bar::{baz::{}, baz1::{}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{baz::{}, baz1::{}}}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:33:5 - | -LL | use foo::{bar::{baz::{}, baz1::{}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{baz::{}, baz1::{}}}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:33:5 + --> $DIR/edition-lint-nested-empty-paths.rs:27:5 | LL | use foo::{bar::{baz::{}, baz1::{}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{baz::{}, baz1::{}}}` @@ -85,7 +40,7 @@ LL | use foo::{bar::{baz::{}, baz1::{}}}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-empty-paths.rs:33:5 + --> $DIR/edition-lint-nested-empty-paths.rs:27:5 | LL | use foo::{bar::{baz::{}, baz1::{}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{bar::{baz::{}, baz1::{}}}` @@ -93,5 +48,5 @@ LL | use foo::{bar::{baz::{}, baz1::{}}}; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! = note: for more information, see issue #53130 -error: aborting due to 10 previous errors +error: aborting due to 5 previous errors diff --git a/src/test/ui/rust-2018/edition-lint-nested-paths.fixed b/src/test/ui/rust-2018/edition-lint-nested-paths.fixed index 4eb1184cb6dff..c4546f8c82129 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-paths.fixed +++ b/src/test/ui/rust-2018/edition-lint-nested-paths.fixed @@ -8,10 +8,6 @@ use crate::foo::{a, b}; //~| this is accepted in the current edition //~| ERROR absolute paths must start with //~| this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| this is accepted in the current edition mod foo { crate fn a() {} @@ -29,8 +25,6 @@ fn main() { //~| this is accepted in the current edition //~| ERROR absolute paths must start with //~| this is accepted in the current edition - //~| ERROR absolute paths must start with - //~| this is accepted in the current edition x::a(); c(); } diff --git a/src/test/ui/rust-2018/edition-lint-nested-paths.rs b/src/test/ui/rust-2018/edition-lint-nested-paths.rs index 2a358224d9bf9..a7e34e407a382 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-paths.rs +++ b/src/test/ui/rust-2018/edition-lint-nested-paths.rs @@ -8,10 +8,6 @@ use foo::{a, b}; //~| this is accepted in the current edition //~| ERROR absolute paths must start with //~| this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| this is accepted in the current edition -//~| ERROR absolute paths must start with -//~| this is accepted in the current edition mod foo { crate fn a() {} @@ -29,8 +25,6 @@ fn main() { //~| this is accepted in the current edition //~| ERROR absolute paths must start with //~| this is accepted in the current edition - //~| ERROR absolute paths must start with - //~| this is accepted in the current edition x::a(); c(); } diff --git a/src/test/ui/rust-2018/edition-lint-nested-paths.stderr b/src/test/ui/rust-2018/edition-lint-nested-paths.stderr index 3d596022b0af5..24b17f212eb15 100644 --- a/src/test/ui/rust-2018/edition-lint-nested-paths.stderr +++ b/src/test/ui/rust-2018/edition-lint-nested-paths.stderr @@ -22,34 +22,7 @@ LL | use foo::{a, b}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-paths.rs:6:5 - | -LL | use foo::{a, b}; - | ^^^^^^^^^^^ help: use `crate`: `crate::foo::{a, b}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-paths.rs:6:5 - | -LL | use foo::{a, b}; - | ^^^^^^^^^^^ help: use `crate`: `crate::foo::{a, b}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-paths.rs:27:13 - | -LL | use foo::{self as x, c}; - | ^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{self as x, c}` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-paths.rs:27:13 + --> $DIR/edition-lint-nested-paths.rs:23:13 | LL | use foo::{self as x, c}; | ^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{self as x, c}` @@ -58,7 +31,7 @@ LL | use foo::{self as x, c}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-nested-paths.rs:27:13 + --> $DIR/edition-lint-nested-paths.rs:23:13 | LL | use foo::{self as x, c}; | ^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::{self as x, c}` @@ -66,5 +39,5 @@ LL | use foo::{self as x, c}; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! = note: for more information, see issue #53130 -error: aborting due to 7 previous errors +error: aborting due to 4 previous errors diff --git a/src/test/ui/rust-2018/edition-lint-paths.fixed b/src/test/ui/rust-2018/edition-lint-paths.fixed index 46adf02a39197..47f82c51daeaa 100644 --- a/src/test/ui/rust-2018/edition-lint-paths.fixed +++ b/src/test/ui/rust-2018/edition-lint-paths.fixed @@ -12,8 +12,6 @@ pub mod foo { use crate::bar::Bar; //~^ ERROR absolute //~| WARN this is accepted in the current edition - //~| ERROR absolute - //~| WARN this is accepted in the current edition use super::bar::Bar2; use crate::bar::Bar3; @@ -42,8 +40,6 @@ pub mod foo { use crate::bar::Bar; //~^ ERROR absolute //~| WARN this is accepted in the current edition -//~| ERROR absolute -//~| WARN this is accepted in the current edition pub mod bar { use edition_lint_paths as foo; @@ -61,8 +57,6 @@ mod baz { impl crate::foo::SomeTrait for u32 {} //~^ ERROR absolute //~| WARN this is accepted in the current edition -//~| ERROR absolute -//~| WARN this is accepted in the current edition fn main() { let x = crate::bar::Bar; diff --git a/src/test/ui/rust-2018/edition-lint-paths.rs b/src/test/ui/rust-2018/edition-lint-paths.rs index f70bf90d6816a..e278983da4a90 100644 --- a/src/test/ui/rust-2018/edition-lint-paths.rs +++ b/src/test/ui/rust-2018/edition-lint-paths.rs @@ -12,8 +12,6 @@ pub mod foo { use bar::Bar; //~^ ERROR absolute //~| WARN this is accepted in the current edition - //~| ERROR absolute - //~| WARN this is accepted in the current edition use super::bar::Bar2; use crate::bar::Bar3; @@ -42,8 +40,6 @@ pub mod foo { use bar::Bar; //~^ ERROR absolute //~| WARN this is accepted in the current edition -//~| ERROR absolute -//~| WARN this is accepted in the current edition pub mod bar { use edition_lint_paths as foo; @@ -61,8 +57,6 @@ mod baz { impl ::foo::SomeTrait for u32 {} //~^ ERROR absolute //~| WARN this is accepted in the current edition -//~| ERROR absolute -//~| WARN this is accepted in the current edition fn main() { let x = ::bar::Bar; diff --git a/src/test/ui/rust-2018/edition-lint-paths.stderr b/src/test/ui/rust-2018/edition-lint-paths.stderr index 481c68e103363..1ded8cd3694c0 100644 --- a/src/test/ui/rust-2018/edition-lint-paths.stderr +++ b/src/test/ui/rust-2018/edition-lint-paths.stderr @@ -13,16 +13,7 @@ LL | #![deny(absolute_paths_not_starting_with_crate)] = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:12:9 - | -LL | use bar::Bar; - | ^^^^^^^^ help: use `crate`: `crate::bar::Bar` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:21:9 + --> $DIR/edition-lint-paths.rs:19:9 | LL | use bar; | ^^^ help: use `crate`: `crate::bar` @@ -31,7 +22,7 @@ LL | use bar; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:27:9 + --> $DIR/edition-lint-paths.rs:25:9 | LL | use {main, Bar as SomethingElse}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::{main, Bar as SomethingElse}` @@ -40,7 +31,7 @@ LL | use {main, Bar as SomethingElse}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:27:9 + --> $DIR/edition-lint-paths.rs:25:9 | LL | use {main, Bar as SomethingElse}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::{main, Bar as SomethingElse}` @@ -49,7 +40,7 @@ LL | use {main, Bar as SomethingElse}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:27:9 + --> $DIR/edition-lint-paths.rs:25:9 | LL | use {main, Bar as SomethingElse}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::{main, Bar as SomethingElse}` @@ -58,7 +49,7 @@ LL | use {main, Bar as SomethingElse}; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:42:5 + --> $DIR/edition-lint-paths.rs:40:5 | LL | use bar::Bar; | ^^^^^^^^ help: use `crate`: `crate::bar::Bar` @@ -67,16 +58,7 @@ LL | use bar::Bar; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:42:5 - | -LL | use bar::Bar; - | ^^^^^^^^ help: use `crate`: `crate::bar::Bar` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:56:9 + --> $DIR/edition-lint-paths.rs:52:9 | LL | use *; | ^ help: use `crate`: `crate::*` @@ -85,16 +67,7 @@ LL | use *; = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:61:6 - | -LL | impl ::foo::SomeTrait for u32 {} - | ^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::SomeTrait` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:61:6 + --> $DIR/edition-lint-paths.rs:57:6 | LL | impl ::foo::SomeTrait for u32 {} | ^^^^^^^^^^^^^^^^ help: use `crate`: `crate::foo::SomeTrait` @@ -103,7 +76,7 @@ LL | impl ::foo::SomeTrait for u32 {} = note: for more information, see issue #53130 error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/edition-lint-paths.rs:68:13 + --> $DIR/edition-lint-paths.rs:62:13 | LL | let x = ::bar::Bar; | ^^^^^^^^^^ help: use `crate`: `crate::bar::Bar` @@ -111,5 +84,5 @@ LL | let x = ::bar::Bar; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! = note: for more information, see issue #53130 -error: aborting due to 12 previous errors +error: aborting due to 9 previous errors diff --git a/src/test/ui/rust-2018/extern-crate-rename.fixed b/src/test/ui/rust-2018/extern-crate-rename.fixed index 05b881a9b08cf..ea832ef3e7d70 100644 --- a/src/test/ui/rust-2018/extern-crate-rename.fixed +++ b/src/test/ui/rust-2018/extern-crate-rename.fixed @@ -12,8 +12,6 @@ extern crate edition_lint_paths as my_crate; use crate::my_crate::foo; //~^ ERROR absolute paths must start //~| WARNING this is accepted in the current edition -//~| ERROR absolute paths must start -//~| WARNING this is accepted in the current edition fn main() { foo(); diff --git a/src/test/ui/rust-2018/extern-crate-rename.rs b/src/test/ui/rust-2018/extern-crate-rename.rs index 6e327be193bf1..b1f617dd88478 100644 --- a/src/test/ui/rust-2018/extern-crate-rename.rs +++ b/src/test/ui/rust-2018/extern-crate-rename.rs @@ -12,8 +12,6 @@ extern crate edition_lint_paths as my_crate; use my_crate::foo; //~^ ERROR absolute paths must start //~| WARNING this is accepted in the current edition -//~| ERROR absolute paths must start -//~| WARNING this is accepted in the current edition fn main() { foo(); diff --git a/src/test/ui/rust-2018/extern-crate-rename.stderr b/src/test/ui/rust-2018/extern-crate-rename.stderr index f2f379ca396b5..4bccbc51223f3 100644 --- a/src/test/ui/rust-2018/extern-crate-rename.stderr +++ b/src/test/ui/rust-2018/extern-crate-rename.stderr @@ -12,14 +12,5 @@ LL | #![deny(absolute_paths_not_starting_with_crate)] = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! = note: for more information, see issue #53130 -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/extern-crate-rename.rs:12:5 - | -LL | use my_crate::foo; - | ^^^^^^^^^^^^^ help: use `crate`: `crate::my_crate::foo` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: aborting due to 2 previous errors +error: aborting due to previous error diff --git a/src/test/ui/rust-2018/extern-crate-submod.fixed b/src/test/ui/rust-2018/extern-crate-submod.fixed index fdbd893abed82..9b0b0dd8ee1d4 100644 --- a/src/test/ui/rust-2018/extern-crate-submod.fixed +++ b/src/test/ui/rust-2018/extern-crate-submod.fixed @@ -19,9 +19,6 @@ mod m { use crate::m::edition_lint_paths::foo; //~^ ERROR absolute paths must start //~| WARNING this is accepted in the current edition -//~| ERROR absolute paths must start -//~| WARNING this is accepted in the current edition - fn main() { foo(); diff --git a/src/test/ui/rust-2018/extern-crate-submod.rs b/src/test/ui/rust-2018/extern-crate-submod.rs index c2b915849f022..dfce9128c5114 100644 --- a/src/test/ui/rust-2018/extern-crate-submod.rs +++ b/src/test/ui/rust-2018/extern-crate-submod.rs @@ -19,9 +19,6 @@ mod m { use m::edition_lint_paths::foo; //~^ ERROR absolute paths must start //~| WARNING this is accepted in the current edition -//~| ERROR absolute paths must start -//~| WARNING this is accepted in the current edition - fn main() { foo(); diff --git a/src/test/ui/rust-2018/extern-crate-submod.stderr b/src/test/ui/rust-2018/extern-crate-submod.stderr index c4c3168bc97df..3c75319aedaed 100644 --- a/src/test/ui/rust-2018/extern-crate-submod.stderr +++ b/src/test/ui/rust-2018/extern-crate-submod.stderr @@ -12,14 +12,5 @@ LL | #![deny(absolute_paths_not_starting_with_crate)] = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! = note: for more information, see issue #53130 -error: absolute paths must start with `self`, `super`, `crate`, or an external crate name in the 2018 edition - --> $DIR/extern-crate-submod.rs:19:5 - | -LL | use m::edition_lint_paths::foo; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `crate`: `crate::m::edition_lint_paths::foo` - | - = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2018! - = note: for more information, see issue #53130 - -error: aborting due to 2 previous errors +error: aborting due to previous error From 15a8b981e08bb942a439b680da93bad1dd18843e Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 24 Mar 2022 00:03:12 +0300 Subject: [PATCH 2/4] resolve: Optimize path resolution for rustdoc Do not construct or pass unused data --- compiler/rustc_resolve/src/lib.rs | 75 ++++--------------- .../passes/collect_intra_doc_links.rs | 28 +++---- .../passes/collect_intra_doc_links/early.rs | 27 +++---- 3 files changed, 38 insertions(+), 92 deletions(-) diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 5a87dede54e36..f8bc1f804719a 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -71,7 +71,7 @@ use rustc_span::{Span, DUMMY_SP}; use smallvec::{smallvec, SmallVec}; use std::cell::{Cell, RefCell}; use std::collections::BTreeSet; -use std::{cmp, fmt, iter, mem, ptr}; +use std::{cmp, fmt, mem, ptr}; use tracing::debug; use diagnostics::{extend_span_to_previous_binding, find_span_of_binding_until_next_binding}; @@ -3309,82 +3309,39 @@ impl<'a> Resolver<'a> { }) } - /// Rustdoc uses this to resolve things in a recoverable way. `ResolutionError<'a>` + /// Rustdoc uses this to resolve doc link paths in a recoverable way. `PathResult<'a>` /// isn't something that can be returned because it can't be made to live that long, /// and also it's a private type. Fortunately rustdoc doesn't need to know the error, /// just that an error occurred. - // FIXME(Manishearth): intra-doc links won't get warned of epoch changes. - pub fn resolve_str_path_error( + pub fn resolve_rustdoc_path( &mut self, - span: Span, path_str: &str, ns: Namespace, module_id: DefId, - ) -> Result<(ast::Path, Res), ()> { - let path = if path_str.starts_with("::") { - ast::Path { - span, - segments: iter::once(Ident::with_dummy_span(kw::PathRoot)) - .chain(path_str.split("::").skip(1).map(Ident::from_str)) - .map(|i| self.new_ast_path_segment(i)) - .collect(), - tokens: None, - } - } else { - ast::Path { - span, - segments: path_str - .split("::") - .map(Ident::from_str) - .map(|i| self.new_ast_path_segment(i)) - .collect(), - tokens: None, - } - }; - let module = self.expect_module(module_id); - let parent_scope = &ParentScope::module(module, self); - let res = self.resolve_ast_path(&path, ns, parent_scope).map_err(|_| ())?; - Ok((path, res)) - } + ) -> Option { + let mut segments = + Vec::from_iter(path_str.split("::").map(Ident::from_str).map(Segment::from_ident)); + if path_str.starts_with("::") { + segments[0].ident.name = kw::PathRoot; + } - // Resolve a path passed from rustdoc or HIR lowering. - fn resolve_ast_path( - &mut self, - path: &ast::Path, - ns: Namespace, - parent_scope: &ParentScope<'a>, - ) -> Result)> { + let module = self.expect_module(module_id); match self.resolve_path( - &Segment::from_path(path), + &segments, Some(ns), - parent_scope, - path.span, + &ParentScope::module(module, self), + DUMMY_SP, CrateLint::No, ) { - PathResult::Module(ModuleOrUniformRoot::Module(module)) => Ok(module.res().unwrap()), + PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()), PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { - Ok(path_res.base_res()) + Some(path_res.base_res()) } - PathResult::NonModule(..) => Err(( - path.span, - ResolutionError::FailedToResolve { - label: String::from("type-relative paths are not supported in this context"), - suggestion: None, - }, - )), + PathResult::NonModule(..) | PathResult::Failed { .. } => None, PathResult::Module(..) | PathResult::Indeterminate => unreachable!(), - PathResult::Failed { span, label, suggestion, .. } => { - Err((span, ResolutionError::FailedToResolve { label, suggestion })) - } } } - fn new_ast_path_segment(&mut self, ident: Ident) -> ast::PathSegment { - let mut seg = ast::PathSegment::from_ident(ident); - seg.id = self.next_node_id(); - seg - } - // For rustdoc. pub fn graph_root(&self) -> Module<'a> { self.graph_root diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 3d8a62d50e06d..5b14aca064e8a 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -487,12 +487,10 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { module_id: DefId, ) -> Result> { self.cx.enter_resolver(|resolver| { - // NOTE: this needs 2 separate lookups because `resolve_str_path_error` doesn't take + // NOTE: this needs 2 separate lookups because `resolve_rustdoc_path` doesn't take // lexical scope into account (it ignores all macros not defined at the mod-level) debug!("resolving {} as a macro in the module {:?}", path_str, module_id); - if let Ok((_, res)) = - resolver.resolve_str_path_error(DUMMY_SP, path_str, MacroNS, module_id) - { + if let Some(res) = resolver.resolve_rustdoc_path(path_str, MacroNS, module_id) { // don't resolve builtins like `#[derive]` if let Ok(res) = res.try_into() { return Ok(res); @@ -540,10 +538,10 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { }) } - /// Convenience wrapper around `resolve_str_path_error`. + /// Convenience wrapper around `resolve_rustdoc_path`. /// /// This also handles resolving `true` and `false` as booleans. - /// NOTE: `resolve_str_path_error` knows only about paths, not about types. + /// NOTE: `resolve_rustdoc_path` knows only about paths, not about types. /// Associated items will never be resolved by this function. fn resolve_path( &self, @@ -556,18 +554,14 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { return res; } - let result = self.cx.enter_resolver(|resolver| { - resolver - .resolve_str_path_error(DUMMY_SP, path_str, ns, module_id) - .and_then(|(_, res)| res.try_into()) - }); + // Resolver doesn't know about true, false, and types that aren't paths (e.g. `()`). + let result = self + .cx + .enter_resolver(|resolver| resolver.resolve_rustdoc_path(path_str, ns, module_id)) + .and_then(|res| res.try_into().ok()) + .or_else(|| resolve_primitive(path_str, ns)); debug!("{} resolved to {:?} in namespace {:?}", path_str, result, ns); - match result { - // resolver doesn't know about true, false, and types that aren't paths (e.g. `()`) - // manually as bool - Err(()) => resolve_primitive(path_str, ns), - Ok(res) => Some(res), - } + result } /// Resolves a string as a path within a particular namespace. Returns an diff --git a/src/librustdoc/passes/collect_intra_doc_links/early.rs b/src/librustdoc/passes/collect_intra_doc_links/early.rs index 1d28bbde79c13..30636faf98c1f 100644 --- a/src/librustdoc/passes/collect_intra_doc_links/early.rs +++ b/src/librustdoc/passes/collect_intra_doc_links/early.rs @@ -13,7 +13,7 @@ use rustc_hir::TraitCandidate; use rustc_middle::ty::{DefIdTree, Visibility}; use rustc_resolve::{ParentScope, Resolver}; use rustc_session::config::Externs; -use rustc_span::{Span, SyntaxContext, DUMMY_SP}; +use rustc_span::SyntaxContext; use std::collections::hash_map::Entry; use std::mem; @@ -39,7 +39,7 @@ crate fn early_resolve_intra_doc_links( // Overridden `visit_item` below doesn't apply to the crate root, // so we have to visit its attributes and reexports separately. - loader.load_links_in_attrs(&krate.attrs, krate.spans.inner_span); + loader.load_links_in_attrs(&krate.attrs); loader.process_module_children_or_reexports(CRATE_DEF_ID.to_def_id()); visit::walk_crate(&mut loader, krate); loader.add_foreign_traits_in_scope(); @@ -49,12 +49,7 @@ crate fn early_resolve_intra_doc_links( // DO NOT REMOVE THIS without first testing on the reproducer in // https://github.com/jyn514/objr/commit/edcee7b8124abf0e4c63873e8422ff81beb11ebb for (extern_name, _) in externs.iter().filter(|(_, entry)| entry.add_prelude) { - let _ = loader.resolver.resolve_str_path_error( - DUMMY_SP, - extern_name, - TypeNS, - CRATE_DEF_ID.to_def_id(), - ); + loader.resolver.resolve_rustdoc_path(extern_name, TypeNS, CRATE_DEF_ID.to_def_id()); } ResolverCaches { @@ -151,7 +146,7 @@ impl IntraLinkCrateLoader<'_, '_> { } } - fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute], span: Span) { + fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute]) { // FIXME: this needs to consider reexport inlining. let attrs = clean::Attributes::from_ast(attrs, None); for (parent_module, doc) in attrs.collapsed_doc_value_by_module_level() { @@ -165,7 +160,7 @@ impl IntraLinkCrateLoader<'_, '_> { } else { continue; }; - let _ = self.resolver.resolve_str_path_error(span, &path_str, TypeNS, module_id); + self.resolver.resolve_rustdoc_path(&path_str, TypeNS, module_id); } } } @@ -201,7 +196,7 @@ impl Visitor<'_> for IntraLinkCrateLoader<'_, '_> { // loaded, even if the module itself has no doc comments. self.add_traits_in_parent_scope(self.current_mod.to_def_id()); - self.load_links_in_attrs(&item.attrs, item.span); + self.load_links_in_attrs(&item.attrs); self.process_module_children_or_reexports(self.current_mod.to_def_id()); visit::walk_item(self, item); @@ -216,28 +211,28 @@ impl Visitor<'_> for IntraLinkCrateLoader<'_, '_> { } _ => {} } - self.load_links_in_attrs(&item.attrs, item.span); + self.load_links_in_attrs(&item.attrs); visit::walk_item(self, item); } } fn visit_assoc_item(&mut self, item: &ast::AssocItem, ctxt: AssocCtxt) { - self.load_links_in_attrs(&item.attrs, item.span); + self.load_links_in_attrs(&item.attrs); visit::walk_assoc_item(self, item, ctxt) } fn visit_foreign_item(&mut self, item: &ast::ForeignItem) { - self.load_links_in_attrs(&item.attrs, item.span); + self.load_links_in_attrs(&item.attrs); visit::walk_foreign_item(self, item) } fn visit_variant(&mut self, v: &ast::Variant) { - self.load_links_in_attrs(&v.attrs, v.span); + self.load_links_in_attrs(&v.attrs); visit::walk_variant(self, v) } fn visit_field_def(&mut self, field: &ast::FieldDef) { - self.load_links_in_attrs(&field.attrs, field.span); + self.load_links_in_attrs(&field.attrs); visit::walk_field_def(self, field) } From 74d079d566255e83ccfdb7da9494079a1cf2c086 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 24 Mar 2022 00:32:00 +0300 Subject: [PATCH 3/4] resolve: Stop passing unused spans and node ids to path resolution functions --- .../rustc_resolve/src/build_reduced_graph.rs | 6 +- compiler/rustc_resolve/src/diagnostics.rs | 23 +-- compiler/rustc_resolve/src/imports.rs | 84 +++------ compiler/rustc_resolve/src/late.rs | 99 +++++------ .../rustc_resolve/src/late/diagnostics.rs | 34 ++-- compiler/rustc_resolve/src/lib.rs | 161 ++++++++---------- compiler/rustc_resolve/src/macros.rs | 48 ++---- src/bootstrap/native.rs | 2 +- 8 files changed, 189 insertions(+), 268 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 924e2d43785de..c1ddff3d03143 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -296,8 +296,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { &segments, Some(TypeNS), parent_scope, - path.span, - if speculative { CrateLint::No } else { CrateLint::SimplePath(id) }, + if speculative { CrateLint::No } else { CrateLint::SimplePath(id, path.span) }, ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => { let res = module.res().expect("visibility resolved to unnamed block"); @@ -1130,8 +1129,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { ident, MacroNS, &self.parent_scope, - false, - ident.span, + None, ); if let Ok(binding) = result { let import = macro_use_import(self, ident.span); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e678ac9d518f5..d407a31c0eaf5 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1076,9 +1076,8 @@ impl<'a> Resolver<'a> { ident, ScopeSet::All(ns, false), &parent_scope, + None, false, - false, - ident.span, ) { let desc = match binding.res() { Res::Def(DefKind::Macro(MacroKind::Bang), _) => { @@ -1405,10 +1404,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> { _ => return None, } - self.make_missing_self_suggestion(span, path.clone(), parent_scope) - .or_else(|| self.make_missing_crate_suggestion(span, path.clone(), parent_scope)) - .or_else(|| self.make_missing_super_suggestion(span, path.clone(), parent_scope)) - .or_else(|| self.make_external_crate_suggestion(span, path, parent_scope)) + self.make_missing_self_suggestion(path.clone(), parent_scope) + .or_else(|| self.make_missing_crate_suggestion(path.clone(), parent_scope)) + .or_else(|| self.make_missing_super_suggestion(path.clone(), parent_scope)) + .or_else(|| self.make_external_crate_suggestion(path, parent_scope)) } /// Suggest a missing `self::` if that resolves to an correct module. @@ -1420,13 +1419,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> { /// ``` fn make_missing_self_suggestion( &mut self, - span: Span, mut path: Vec, parent_scope: &ParentScope<'b>, ) -> Option<(Vec, Vec)> { // Replace first ident with `self` and check if that is valid. path[0].ident.name = kw::SelfLower; - let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1440,13 +1438,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> { /// ``` fn make_missing_crate_suggestion( &mut self, - span: Span, mut path: Vec, parent_scope: &ParentScope<'b>, ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Crate; - let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some(( @@ -1472,13 +1469,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> { /// ``` fn make_missing_super_suggestion( &mut self, - span: Span, mut path: Vec, parent_scope: &ParentScope<'b>, ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Super; - let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1495,7 +1491,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> { /// name as the first part of path. fn make_external_crate_suggestion( &mut self, - span: Span, mut path: Vec, parent_scope: &ParentScope<'b>, ) -> Option<(Vec, Vec)> { @@ -1513,7 +1508,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { for name in extern_crate_names.into_iter() { // Replace first ident with a crate name and check if that is valid. path[0].ident.name = name; - let result = self.r.resolve_path(&path, None, parent_scope, span, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); debug!( "make_external_crate_suggestion: name={:?} path={:?} result={:?}", name, path, result diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 87c06559ccaa7..34bd39e711e90 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -175,8 +175,7 @@ impl<'a> Resolver<'a> { ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: bool, - path_span: Span, + record_used: Option, ) -> Result<&'a NameBinding<'a>, Determinacy> { self.resolve_ident_in_module_unadjusted_ext( module, @@ -185,7 +184,6 @@ impl<'a> Resolver<'a> { parent_scope, false, record_used, - path_span, ) .map_err(|(determinacy, _)| determinacy) } @@ -199,8 +197,7 @@ impl<'a> Resolver<'a> { ns: Namespace, parent_scope: &ParentScope<'a>, restricted_shadowing: bool, - record_used: bool, - path_span: Span, + record_used: Option, ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> { let module = match module { ModuleOrUniformRoot::Module(module) => module, @@ -211,8 +208,7 @@ impl<'a> Resolver<'a> { ScopeSet::AbsolutePath(ns), parent_scope, record_used, - record_used, - path_span, + record_used.is_some(), ); return binding.map_err(|determinacy| (determinacy, Weak::No)); } @@ -220,7 +216,8 @@ impl<'a> Resolver<'a> { assert!(!restricted_shadowing); return if ns != TypeNS { Err((Determined, Weak::No)) - } else if let Some(binding) = self.extern_prelude_get(ident, !record_used) { + } else if let Some(binding) = self.extern_prelude_get(ident, record_used.is_none()) + { Ok(binding) } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() { // Macro-expanded `extern crate` items can add names to extern prelude. @@ -251,8 +248,7 @@ impl<'a> Resolver<'a> { scopes, parent_scope, record_used, - record_used, - path_span, + record_used.is_some(), ); return binding.map_err(|determinacy| (determinacy, Weak::No)); } @@ -262,7 +258,7 @@ impl<'a> Resolver<'a> { let resolution = self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports. - if let Some(binding) = resolution.binding { + if let Some(binding) = resolution.binding && let Some(path_span) = record_used { if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT { if let NameBindingKind::Res(_, true) = binding.kind { self.macro_expanded_macro_export_errors.insert((path_span, binding.span)); @@ -280,7 +276,7 @@ impl<'a> Resolver<'a> { if usable { Ok(binding) } else { Err((Determined, Weak::No)) } }; - if record_used { + if let Some(path_span) = record_used { return resolution .binding .and_then(|binding| { @@ -353,14 +349,8 @@ impl<'a> Resolver<'a> { let ImportKind::Single { source: ident, .. } = single_import.kind else { unreachable!(); }; - match self.resolve_ident_in_module( - module, - ident, - ns, - &single_import.parent_scope, - false, - path_span, - ) { + match self.resolve_ident_in_module(module, ident, ns, &single_import.parent_scope, None) + { Err(Determined) => continue, Ok(binding) if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) => @@ -434,8 +424,7 @@ impl<'a> Resolver<'a> { ident, ns, adjusted_parent_scope, - false, - path_span, + None, ); match result { @@ -783,13 +772,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> { // For better failure detection, pretend that the import will // not define any names while resolving its module path. let orig_vis = import.vis.replace(ty::Visibility::Invisible); - let path_res = self.r.resolve_path( - &import.module_path, - None, - &import.parent_scope, - import.span, - CrateLint::No, - ); + let path_res = + self.r.resolve_path(&import.module_path, None, &import.parent_scope, CrateLint::No); import.vis.set(orig_vis); match path_res { @@ -828,8 +812,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { source, ns, &import.parent_scope, - false, - import.span, + None, ); import.vis.set(orig_vis); source_bindings[ns].set(binding); @@ -882,15 +865,13 @@ impl<'a, 'b> ImportResolver<'a, 'b> { _ => None, }; let prev_ambiguity_errors_len = self.r.ambiguity_errors.len(); - let crate_lint = - CrateLint::UsePath { root_id: import.root_id, root_span: import.root_span }; - let path_res = self.r.resolve_path( - &import.module_path, - None, - &import.parent_scope, - import.span, - crate_lint, - ); + let crate_lint = CrateLint::UsePath { + root_id: import.root_id, + root_span: import.root_span, + path_span: import.span, + }; + let path_res = + self.r.resolve_path(&import.module_path, None, &import.parent_scope, crate_lint); let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len; if let Some(orig_unusable_binding) = orig_unusable_binding { self.r.unusable_binding = orig_unusable_binding; @@ -977,12 +958,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { // 2 segments, so the `resolve_path` above won't trigger it. let mut full_path = import.module_path.clone(); full_path.push(Segment::from_ident(Ident::empty())); - self.r.lint_if_path_starts_with_module( - crate_lint, - &full_path, - import.span, - None, - ); + self.r.lint_if_path_starts_with_module(crate_lint, &full_path, None); } if let ModuleOrUniformRoot::Module(module) = module { @@ -1020,8 +996,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ident, ns, &import.parent_scope, - true, - import.span, + Some(import.span), ); this.last_import_segment = orig_last_import_segment; this.unusable_binding = orig_unusable_binding; @@ -1082,8 +1057,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ident, ns, &import.parent_scope, - true, - import.span, + Some(import.span), ); if binding.is_ok() { all_ns_failed = false; @@ -1249,12 +1223,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { full_path.push(Segment::from_ident(ident)); self.r.per_ns(|this, ns| { if let Ok(binding) = source_bindings[ns].get() { - this.lint_if_path_starts_with_module( - crate_lint, - &full_path, - import.span, - Some(binding), - ); + this.lint_if_path_starts_with_module(crate_lint, &full_path, Some(binding)); } }); } @@ -1310,9 +1279,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> { target, ScopeSet::All(ns, false), &import.parent_scope, + None, false, - false, - import.span, ) { Ok(other_binding) => { is_redundant[ns] = Some( diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 511dc285cb4f4..995a48be407b1 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -483,7 +483,11 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { TyKind::ImplicitSelf => { let self_ty = Ident::with_dummy_span(kw::SelfUpper); let res = self - .resolve_ident_in_lexical_scope(self_ty, TypeNS, Some(ty.id), ty.span) + .resolve_ident_in_lexical_scope( + self_ty, + TypeNS, + CrateLint::SimplePath(ty.id, ty.span), + ) .map_or(Res::Err, |d| d.res()); self.r.record_partial_res(ty.id, PartialRes::new(res)); } @@ -675,8 +679,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { self.resolve_ident_in_lexical_scope( path.segments[0].ident, ns, - None, - path.span, + CrateLint::No, ) .is_some() }; @@ -751,15 +754,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { &mut self, ident: Ident, ns: Namespace, - record_used_id: Option, - path_span: Span, + crate_lint: CrateLint, ) -> Option> { self.r.resolve_ident_in_lexical_scope( ident, ns, &self.parent_scope, - record_used_id, - path_span, + crate_lint, &self.ribs[ns], ) } @@ -768,14 +769,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { &mut self, path: &[Segment], opt_ns: Option, // `None` indicates a module path in import - path_span: Span, crate_lint: CrateLint, ) -> PathResult<'a> { self.r.resolve_path_with_ribs( path, opt_ns, &self.parent_scope, - path_span, crate_lint, Some(&self.ribs), ) @@ -941,15 +940,15 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }; for &ns in nss { - match self.resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) { + match self.resolve_ident_in_lexical_scope(ident, ns, CrateLint::No) { Some(LexicalScopeBinding::Res(..)) => { report_error(self, ns); } Some(LexicalScopeBinding::Item(binding)) => { let orig_unusable_binding = replace(&mut self.r.unusable_binding, Some(binding)); - if let Some(LexicalScopeBinding::Res(..)) = self - .resolve_ident_in_lexical_scope(ident, ns, None, use_tree.prefix.span) + if let Some(LexicalScopeBinding::Res(..)) = + self.resolve_ident_in_lexical_scope(ident, ns, CrateLint::No) { report_error(self, ns); } @@ -1244,12 +1243,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { if let Some(trait_ref) = opt_trait_ref { let path: Vec<_> = Segment::from_path(&trait_ref.path); let res = self.smart_resolve_path_fragment( - trait_ref.ref_id, None, &path, - trait_ref.path.span, PathSource::Trait(AliasPossibility::No), - CrateLint::SimplePath(trait_ref.ref_id), + CrateLint::SimplePath(trait_ref.ref_id, trait_ref.path.span), ); if let Some(def_id) = res.base_res().opt_def_id() { new_id = Some(def_id); @@ -1691,7 +1688,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // then fall back to a fresh binding. let has_sub = sub.is_some(); let res = self - .try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub) + .try_resolve_as_non_binding(pat_src, bmode, ident, has_sub) .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_pat_span(pat.id, pat.span); @@ -1802,7 +1799,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { fn try_resolve_as_non_binding( &mut self, pat_src: PatternSource, - pat: &Pat, bm: BindingMode, ident: Ident, has_sub: bool, @@ -1812,7 +1808,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // also be interpreted as a path to e.g. a constant, variant, etc. let is_syntactic_ambiguity = !has_sub && bm == BindingMode::ByValue(Mutability::Not); - let ls_binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, None, pat.span)?; + let ls_binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, CrateLint::No)?; let (res, binding) = match ls_binding { LexicalScopeBinding::Item(binding) if is_syntactic_ambiguity && binding.is_ambiguity() => @@ -1901,35 +1897,34 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { source: PathSource<'ast>, ) { self.smart_resolve_path_fragment( - id, qself, &Segment::from_path(path), - path.span, source, - CrateLint::SimplePath(id), + CrateLint::SimplePath(id, path.span), ); } fn smart_resolve_path_fragment( &mut self, - id: NodeId, qself: Option<&QSelf>, path: &[Segment], - span: Span, source: PathSource<'ast>, crate_lint: CrateLint, ) -> PartialRes { tracing::debug!( - "smart_resolve_path_fragment(id={:?}, qself={:?}, path={:?})", - id, + "smart_resolve_path_fragment(qself={:?}, path={:?}, crate_lint={:?})", qself, - path + path, + crate_lint, ); let ns = source.namespace(); + let (id, path_span) = + crate_lint.node_id_and_path_span().expect("unexpected speculative resolution"); let report_errors = |this: &mut Self, res: Option| { if this.should_report_errs() { - let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res); + let (err, candidates) = + this.smart_resolve_report_errors(path, path_span, source, res); let def_id = this.parent_scope.module.nearest_parent_mod(); let instead = res.is_some(); @@ -1967,7 +1962,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }; let (mut err, candidates) = - this.smart_resolve_report_errors(path, span, PathSource::Type, None); + this.smart_resolve_report_errors(path, path_span, PathSource::Type, None); if candidates.is_empty() { err.cancel(); @@ -2015,13 +2010,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { None }; - assert_ne!(crate_lint, CrateLint::No); let partial_res = match self.resolve_qpath_anywhere( - id, qself, path, ns, - span, + path_span, source.defer_to_typeck(), crate_lint, ) { @@ -2050,14 +2043,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std))); std_path.extend(path); if let PathResult::Module(_) | PathResult::NonModule(_) = - self.resolve_path(&std_path, Some(ns), span, CrateLint::No) + self.resolve_path(&std_path, Some(ns), CrateLint::No) { // Check if we wrote `str::from_utf8` instead of `std::str::from_utf8` let item_span = - path.iter().last().map_or(span, |segment| segment.ident.span); + path.iter().last().map_or(path_span, |segment| segment.ident.span); - self.r.confused_type_with_std_module.insert(item_span, span); - self.r.confused_type_with_std_module.insert(span, span); + self.r.confused_type_with_std_module.insert(item_span, path_span); + self.r.confused_type_with_std_module.insert(path_span, path_span); } } @@ -2083,19 +2076,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { partial_res } - fn self_type_is_available(&mut self, span: Span) -> bool { + fn self_type_is_available(&mut self) -> bool { let binding = self.resolve_ident_in_lexical_scope( Ident::with_dummy_span(kw::SelfUpper), TypeNS, - None, - span, + CrateLint::No, ); if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false } } - fn self_value_is_available(&mut self, self_span: Span, path_span: Span) -> bool { + fn self_value_is_available(&mut self, self_span: Span) -> bool { let ident = Ident::new(kw::SelfLower, self_span); - let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, None, path_span); + let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, CrateLint::No); if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false } } @@ -2117,7 +2109,6 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // Resolve in alternative namespaces if resolution in the primary namespace fails. fn resolve_qpath_anywhere( &mut self, - id: NodeId, qself: Option<&QSelf>, path: &[Segment], primary_ns: Namespace, @@ -2129,7 +2120,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { for (i, &ns) in [primary_ns, TypeNS, ValueNS].iter().enumerate() { if i == 0 || ns != primary_ns { - match self.resolve_qpath(id, qself, path, ns, span, crate_lint)? { + match self.resolve_qpath(qself, path, ns, crate_lint)? { Some(partial_res) if partial_res.unresolved_segments() == 0 || defer_to_typeck => { @@ -2162,16 +2153,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { /// Handles paths that may refer to associated items. fn resolve_qpath( &mut self, - id: NodeId, qself: Option<&QSelf>, path: &[Segment], ns: Namespace, - span: Span, crate_lint: CrateLint, ) -> Result, Spanned>> { debug!( - "resolve_qpath(id={:?}, qself={:?}, path={:?}, ns={:?}, span={:?})", - id, qself, path, ns, span, + "resolve_qpath(qself={:?}, path={:?}, ns={:?}, crate_lint={:?})", + qself, path, ns, crate_lint, ); if let Some(qself) = qself { @@ -2201,12 +2190,17 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // contains the full span (the `CrateLint::QPathTrait`). let ns = if qself.position + 1 == path.len() { ns } else { TypeNS }; let partial_res = self.smart_resolve_path_fragment( - id, None, &path[..=qself.position], - span, PathSource::TraitItem(ns), - CrateLint::QPathTrait { qpath_id: id, qpath_span: qself.path_span }, + crate_lint.node_id_and_path_span().map_or( + CrateLint::No, + |(qpath_id, path_span)| CrateLint::QPathTrait { + qpath_id, + qpath_span: qself.path_span, + path_span, + }, + ), ); // The remaining segments (the `C` in our example) will @@ -2218,7 +2212,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ))); } - let result = match self.resolve_path(&path, Some(ns), span, crate_lint) { + let result = match self.resolve_path(&path, Some(ns), crate_lint) { PathResult::NonModule(path_res) => path_res, PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => { PartialRes::new(module.res().unwrap()) @@ -2256,9 +2250,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { && result.base_res() != Res::Err && path[0].ident.name != kw::PathRoot && path[0].ident.name != kw::DollarCrate + && let Some((id, path_span)) = crate_lint.node_id_and_path_span() { let unqualified_result = { - match self.resolve_path(&[*path.last().unwrap()], Some(ns), span, CrateLint::No) { + match self.resolve_path(&[*path.last().unwrap()], Some(ns), CrateLint::No) { PathResult::NonModule(path_res) => path_res.base_res(), PathResult::Module(ModuleOrUniformRoot::Module(module)) => { module.res().unwrap() @@ -2268,7 +2263,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }; if result.base_res() == unqualified_result { let lint = lint::builtin::UNUSED_QUALIFICATIONS; - self.r.lint_buffer.buffer_lint(lint, id, span, "unnecessary qualification") + self.r.lint_buffer.buffer_lint(lint, id, path_span, "unnecessary qualification") } } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 3c6caa7953771..fc04ce6dfb0a5 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -187,12 +187,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { (String::new(), "the crate root".to_string()) } else { let mod_path = &path[..path.len() - 1]; - let mod_prefix = - match self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No) { - PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(), - _ => None, - } - .map_or_else(String::new, |res| format!("{} ", res.descr())); + let mod_prefix = match self.resolve_path(mod_path, Some(TypeNS), CrateLint::No) { + PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(), + _ => None, + } + .map_or_else(String::new, |res| format!("{} ", res.descr())); (mod_prefix, format!("`{}`", Segment::names_to_string(mod_path))) }; ( @@ -232,7 +231,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { _ => {} } - let is_assoc_fn = self.self_type_is_available(span); + let is_assoc_fn = self.self_type_is_available(); // Emit help message for fake-self from other languages (e.g., `this` in Javascript). if ["this", "my"].contains(&item_str.as_str()) && is_assoc_fn { err.span_suggestion_short( @@ -241,7 +240,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { "self".to_string(), Applicability::MaybeIncorrect, ); - if !self.self_value_is_available(path[0].ident.span, span) { + if !self.self_value_is_available(path[0].ident.span) { if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) = &self.diagnostic_metadata.current_function { @@ -402,9 +401,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { ); } } - if path.len() == 1 && self.self_type_is_available(span) { + if path.len() == 1 && self.self_type_is_available() { if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) { - let self_is_available = self.self_value_is_available(path[0].ident.span, span); + let self_is_available = self.self_value_is_available(path[0].ident.span); match candidate { AssocSuggestion::Field => { if self_is_available { @@ -461,7 +460,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } // Try Levenshtein algorithm. - let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected, span); + let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected); // Try context-dependent help if relaxed lookup didn't work. if let Some(res) = res { if self.smart_resolve_context_dependent_help( @@ -562,7 +561,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } // If the trait has a single item (which wasn't matched by Levenshtein), suggest it - let suggestion = self.get_single_associated_item(&path, span, &source, is_expected); + let suggestion = self.get_single_associated_item(&path, &source, is_expected); self.r.add_typo_suggestion(&mut err, suggestion, ident_span); } if fallback { @@ -641,14 +640,13 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { fn get_single_associated_item( &mut self, path: &[Segment], - span: Span, source: &PathSource<'_>, filter_fn: &impl Fn(Res) -> bool, ) -> Option { if let crate::PathSource::TraitItem(_) = source { let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, None, span, CrateLint::No) + self.resolve_path(mod_path, None, CrateLint::No) { let resolutions = self.r.resolutions(module).borrow(); let targets: Vec<_> = @@ -699,7 +697,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { { // use this to verify that ident is a type param. let Ok(Some(partial_res)) = self.resolve_qpath_anywhere( - bounded_ty.id, None, &Segment::from_path(path), Namespace::TypeNS, @@ -724,7 +721,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { if let ast::TyKind::Path(None, type_param_path) = &ty.peel_refs().kind { // Confirm that the `SelfTy` is a type parameter. let Ok(Some(partial_res)) = self.resolve_qpath_anywhere( - bounded_ty.id, None, &Segment::from_path(type_param_path), Namespace::TypeNS, @@ -1292,8 +1288,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { ident, ns, &self.parent_scope, - false, - module.span, + None, ) { let res = binding.res(); if filter_fn(res) { @@ -1323,7 +1318,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { path: &[Segment], ns: Namespace, filter_fn: &impl Fn(Res) -> bool, - span: Span, ) -> Option { let mut names = Vec::new(); if path.len() == 1 { @@ -1384,7 +1378,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { // Search in module. let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, Some(TypeNS), span, CrateLint::No) + self.resolve_path(mod_path, Some(TypeNS), CrateLint::No) { self.r.add_module_candidates(module, &mut names, &filter_fn); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index f8bc1f804719a..a4fe7676fc459 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -13,6 +13,7 @@ #![feature(drain_filter)] #![feature(bool_to_option)] #![feature(crate_visibility_modifier)] +#![feature(let_chains)] #![feature(let_else)] #![feature(never_type)] #![feature(nll)] @@ -54,9 +55,9 @@ use rustc_index::vec::IndexVec; use rustc_metadata::creader::{CStore, CrateLoader}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::privacy::AccessLevels; -use rustc_middle::span_bug; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, ResolverOutputs}; +use rustc_middle::{bug, span_bug}; use rustc_query_system::ich::StableHashingContext; use rustc_session::cstore::{CrateStore, MetadataLoaderDyn}; use rustc_session::lint; @@ -1949,8 +1950,7 @@ impl<'a> Resolver<'a> { mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used_id: Option, - path_span: Span, + crate_lint: CrateLint, ribs: &[Rib<'a>], ) -> Option> { assert!(ns == TypeNS || ns == ValueNS); @@ -1972,7 +1972,7 @@ impl<'a> Resolver<'a> { let normalized_ident = Ident { span: normalized_span, ..ident }; // Walk backwards up the ribs in scope. - let record_used = record_used_id.is_some(); + let record_used = crate_lint.path_span(); let mut module = self.graph_root; for i in (0..ribs.len()).rev() { debug!("walk rib\n{:?}", ribs[i].bindings); @@ -1987,7 +1987,6 @@ impl<'a> Resolver<'a> { rib_ident, *res, record_used, - path_span, *original_rib_ident_def, ribs, ))); @@ -2015,7 +2014,6 @@ impl<'a> Resolver<'a> { ns, parent_scope, record_used, - path_span, ); if let Ok(binding) = item { // The ident resolves to an item. @@ -2024,11 +2022,10 @@ impl<'a> Resolver<'a> { } self.early_resolve_ident_in_lexical_scope( orig_ident, - ScopeSet::Late(ns, module, record_used_id), + ScopeSet::Late(ns, module, crate_lint.node_id()), parent_scope, record_used, - record_used, - path_span, + record_used.is_some(), ) .ok() .map(LexicalScopeBinding::Item) @@ -2088,10 +2085,9 @@ impl<'a> Resolver<'a> { ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: bool, - path_span: Span, + record_used: Option, ) -> Result<&'a NameBinding<'a>, Determinacy> { - self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, record_used, path_span) + self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, record_used) .map_err(|(determinacy, _)| determinacy) } @@ -2101,8 +2097,7 @@ impl<'a> Resolver<'a> { mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: bool, - path_span: Span, + record_used: Option, ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> { let tmp_parent_scope; let mut adjusted_parent_scope = parent_scope; @@ -2128,7 +2123,6 @@ impl<'a> Resolver<'a> { adjusted_parent_scope, false, record_used, - path_span, ) } @@ -2219,10 +2213,9 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - path_span: Span, crate_lint: CrateLint, ) -> PathResult<'a> { - self.resolve_path_with_ribs(path, opt_ns, parent_scope, path_span, crate_lint, None) + self.resolve_path_with_ribs(path, opt_ns, parent_scope, crate_lint, None) } fn resolve_path_with_ribs( @@ -2230,24 +2223,20 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - path_span: Span, crate_lint: CrateLint, ribs: Option<&PerNS>>>, ) -> PathResult<'a> { - let record_used = crate_lint != CrateLint::No; + debug!("resolve_path(path={:?}, opt_ns={:?}, crate_lint={:?})", path, opt_ns, crate_lint); + + let record_used = crate_lint.path_span(); let mut module = None; let mut allow_super = true; let mut second_binding = None; - debug!( - "resolve_path(path={:?}, opt_ns={:?}, path_span={:?}, crate_lint={:?})", - path, opt_ns, path_span, crate_lint, - ); - for (i, &Segment { ident, id, has_generic_args: _ }) in path.iter().enumerate() { debug!("resolve_path ident {} {:?} {:?}", i, ident, id); let record_segment_res = |this: &mut Self, res| { - if record_used { + if record_used.is_some() { if let Some(id) = id { if !this.partial_res_map.contains_key(&id) { assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id"); @@ -2281,7 +2270,7 @@ impl<'a> Resolver<'a> { continue; } } - return PathResult::failed(ident.span, false, record_used, || { + return PathResult::failed(ident.span, false, record_used.is_some(), || { ("there are too many leading `super` keywords".to_string(), None) }); } @@ -2312,7 +2301,7 @@ impl<'a> Resolver<'a> { // Report special messages for path segment keywords in wrong positions. if ident.is_path_segment_keyword() && i != 0 { - return PathResult::failed(ident.span, false, record_used, || { + return PathResult::failed(ident.span, false, record_used.is_some(), || { let name_str = if name == kw::PathRoot { "crate root".to_string() } else { @@ -2333,14 +2322,7 @@ impl<'a> Resolver<'a> { } let find_binding_in_ns = |this: &mut Self, ns| { let binding = if let Some(module) = module { - this.resolve_ident_in_module( - module, - ident, - ns, - parent_scope, - record_used, - path_span, - ) + this.resolve_ident_in_module(module, ident, ns, parent_scope, record_used) } else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) { let scopes = ScopeSet::All(ns, opt_ns.is_none()); this.early_resolve_ident_in_lexical_scope( @@ -2348,16 +2330,14 @@ impl<'a> Resolver<'a> { scopes, parent_scope, record_used, - record_used, - path_span, + record_used.is_some(), ) } else { match this.resolve_ident_in_lexical_scope( ident, ns, parent_scope, - crate_lint.node_id(), - path_span, + crate_lint, &ribs.unwrap()[ns], ) { // we found a locally-imported or available item/module @@ -2371,7 +2351,7 @@ impl<'a> Resolver<'a> { PartialRes::with_unresolved_segments(res, path.len() - 1), )); } - _ => Err(Determinacy::determined(record_used)), + _ => Err(Determinacy::determined(record_used.is_some())), } }; FindBindingResult::Binding(binding) @@ -2405,25 +2385,25 @@ impl<'a> Resolver<'a> { } else if res == Res::Err { return PathResult::NonModule(PartialRes::new(Res::Err)); } else if opt_ns.is_some() && (is_last || maybe_assoc) { - self.lint_if_path_starts_with_module( - crate_lint, - path, - path_span, - second_binding, - ); + self.lint_if_path_starts_with_module(crate_lint, path, second_binding); return PathResult::NonModule(PartialRes::with_unresolved_segments( res, path.len() - i - 1, )); } else { - return PathResult::failed(ident.span, is_last, record_used, || { - let label = format!( - "`{ident}` is {} {}, not a module", - res.article(), - res.descr() - ); - (label, None) - }); + return PathResult::failed( + ident.span, + is_last, + record_used.is_some(), + || { + let label = format!( + "`{ident}` is {} {}, not a module", + res.article(), + res.descr() + ); + (label, None) + }, + ); } } Err(Undetermined) => return PathResult::Indeterminate, @@ -2437,7 +2417,7 @@ impl<'a> Resolver<'a> { } } - return PathResult::failed(ident.span, is_last, record_used, || { + return PathResult::failed(ident.span, is_last, record_used.is_some(), || { let module_res = match module { Some(ModuleOrUniformRoot::Module(module)) => module.res(), _ => None, @@ -2477,8 +2457,7 @@ impl<'a> Resolver<'a> { ident, ValueNS, parent_scope, - None, - path_span, + CrateLint::No, &ribs.unwrap()[ValueNS], ) { // Name matches a local variable. For example: @@ -2603,12 +2582,12 @@ impl<'a> Resolver<'a> { } } - self.lint_if_path_starts_with_module(crate_lint, path, path_span, second_binding); + self.lint_if_path_starts_with_module(crate_lint, path, second_binding); PathResult::Module(match module { Some(module) => module, None if path.is_empty() => ModuleOrUniformRoot::CurrentScope, - _ => span_bug!(path_span, "resolve_path: non-empty path `{:?}` has no module", path), + _ => bug!("resolve_path: non-empty path `{:?}` has no module", path), }) } @@ -2616,14 +2595,13 @@ impl<'a> Resolver<'a> { &mut self, crate_lint: CrateLint, path: &[Segment], - path_span: Span, second_binding: Option<&NameBinding<'_>>, ) { let (diag_id, diag_span) = match crate_lint { CrateLint::No => return, - CrateLint::SimplePath(id) => (id, path_span), - CrateLint::UsePath { root_id, root_span } => (root_id, root_span), - CrateLint::QPathTrait { qpath_id, qpath_span } => (qpath_id, qpath_span), + CrateLint::SimplePath(id, path_span) => (id, path_span), + CrateLint::UsePath { root_id, root_span, .. } => (root_id, root_span), + CrateLint::QPathTrait { qpath_id, qpath_span, .. } => (qpath_id, qpath_span), }; let first_name = match path.get(0) { @@ -2678,8 +2656,7 @@ impl<'a> Resolver<'a> { rib_index: usize, rib_ident: Ident, mut res: Res, - record_used: bool, - span: Span, + record_used: Option, original_rib_ident_def: Ident, all_ribs: &[Rib<'a>], ) -> Res { @@ -2689,7 +2666,7 @@ impl<'a> Resolver<'a> { // An invalid forward use of a generic parameter from a previous default. if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind { - if record_used { + if let Some(span) = record_used { let res_error = if rib_ident.name == kw::SelfUpper { ResolutionError::SelfInGenericParamDefault } else { @@ -2719,17 +2696,17 @@ impl<'a> Resolver<'a> { // This was an attempt to access an upvar inside a // named function item. This is not allowed, so we // report an error. - if record_used { + if let Some(span) = record_used { // We don't immediately trigger a resolve error, because // we want certain other resolution errors (namely those // emitted for `ConstantItemRibKind` below) to take // precedence. - res_err = Some(CannotCaptureDynamicEnvironmentInFnItem); + res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem)); } } ConstantItemRibKind(_, item) => { // Still doesn't deal with upvars - if record_used { + if let Some(span) = record_used { let (span, resolution_error) = if let Some((ident, constant_item_kind)) = item { let kind_str = match constant_item_kind { @@ -2757,14 +2734,14 @@ impl<'a> Resolver<'a> { return Res::Err; } ConstParamTyRibKind => { - if record_used { + if let Some(span) = record_used { self.report_error(span, ParamInTyOfConstParam(rib_ident.name)); } return Res::Err; } } } - if let Some(res_err) = res_err { + if let Some((span, res_err)) = res_err { self.report_error(span, res_err); return Res::Err; } @@ -2792,7 +2769,7 @@ impl<'a> Resolver<'a> { if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res { res = Res::SelfTy { trait_, alias_to: Some((def, true)) } } else { - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::ParamInNonTrivialAnonConst { @@ -2800,9 +2777,9 @@ impl<'a> Resolver<'a> { is_type: true, }, ); + self.session.delay_span_bug(span, CG_BUG_STR); } - self.session.delay_span_bug(span, CG_BUG_STR); return Res::Err; } } @@ -2814,7 +2791,7 @@ impl<'a> Resolver<'a> { ItemRibKind(has_generic_params) => has_generic_params, FnItemRibKind => HasGenericParams::Yes, ConstParamTyRibKind => { - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::ParamInTyOfConstParam(rib_ident.name), @@ -2824,7 +2801,7 @@ impl<'a> Resolver<'a> { } }; - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::GenericParamsFromOuterFunction( @@ -2858,7 +2835,7 @@ impl<'a> Resolver<'a> { let features = self.session.features_untracked(); // HACK(min_const_generics): We currently only allow `N` or `{ N }`. if !(trivial || features.generic_const_exprs) { - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::ParamInNonTrivialAnonConst { @@ -2866,9 +2843,9 @@ impl<'a> Resolver<'a> { is_type: false, }, ); + self.session.delay_span_bug(span, CG_BUG_STR); } - self.session.delay_span_bug(span, CG_BUG_STR); return Res::Err; } @@ -2878,7 +2855,7 @@ impl<'a> Resolver<'a> { ItemRibKind(has_generic_params) => has_generic_params, FnItemRibKind => HasGenericParams::Yes, ConstParamTyRibKind => { - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::ParamInTyOfConstParam(rib_ident.name), @@ -2889,7 +2866,7 @@ impl<'a> Resolver<'a> { }; // This was an attempt to use a const parameter outside its scope. - if record_used { + if let Some(span) = record_used { self.report_error( span, ResolutionError::GenericParamsFromOuterFunction( @@ -3330,7 +3307,6 @@ impl<'a> Resolver<'a> { &segments, Some(ns), &ParentScope::module(module, self), - DUMMY_SP, CrateLint::No, ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()), @@ -3425,8 +3401,7 @@ impl<'a> Resolver<'a> { ident, ValueNS, parent_scope, - false, - DUMMY_SP, + None ) else { return; }; @@ -3490,29 +3465,37 @@ enum CrateLint { /// This lint applies to some arbitrary path; e.g., `impl ::foo::Bar`. /// In this case, we can take the span of that path. - SimplePath(NodeId), + SimplePath(NodeId, Span), /// This lint comes from a `use` statement. In this case, what we /// care about really is the *root* `use` statement; e.g., if we /// have nested things like `use a::{b, c}`, we care about the /// `use a` part. - UsePath { root_id: NodeId, root_span: Span }, + UsePath { root_id: NodeId, root_span: Span, path_span: Span }, /// This is the "trait item" from a fully qualified path. For example, /// we might be resolving `X::Y::Z` from a path like `::Z`. /// The `path_span` is the span of the to the trait itself (`X::Y`). - QPathTrait { qpath_id: NodeId, qpath_span: Span }, + QPathTrait { qpath_id: NodeId, qpath_span: Span, path_span: Span }, } impl CrateLint { - fn node_id(&self) -> Option { + fn node_id_and_path_span(&self) -> Option<(NodeId, Span)> { match *self { CrateLint::No => None, - CrateLint::SimplePath(id) - | CrateLint::UsePath { root_id: id, .. } - | CrateLint::QPathTrait { qpath_id: id, .. } => Some(id), + CrateLint::SimplePath(id, path_span) + | CrateLint::UsePath { root_id: id, path_span, .. } + | CrateLint::QPathTrait { qpath_id: id, path_span, .. } => Some((id, path_span)), } } + + fn node_id(&self) -> Option { + self.node_id_and_path_span().map(|(id, _)| id) + } + + fn path_span(&self) -> Option { + self.node_id_and_path_span().map(|(_, path_span)| path_span) + } } pub fn provide(providers: &mut Providers) { diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index d30aed71e23e8..e8ba8780526c5 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -415,7 +415,7 @@ impl<'a> ResolverExpand for Resolver<'a> { let mut indeterminate = false; for ns in [TypeNS, ValueNS, MacroNS].iter().copied() { - match self.resolve_path(path, Some(ns), &parent_scope, span, CrateLint::No) { + match self.resolve_path(path, Some(ns), &parent_scope, CrateLint::No) { PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true), PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => { return Ok(true); @@ -575,13 +575,7 @@ impl<'a> Resolver<'a> { } let res = if path.len() > 1 { - let res = match self.resolve_path( - &path, - Some(MacroNS), - parent_scope, - path_span, - CrateLint::No, - ) { + let res = match self.resolve_path(&path, Some(MacroNS), parent_scope, CrateLint::No) { PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { Ok(path_res.base_res()) } @@ -611,9 +605,8 @@ impl<'a> Resolver<'a> { path[0].ident, scope_set, parent_scope, - false, + None, force, - path_span, ); if let Err(Determinacy::Undetermined) = binding { return Err(Determinacy::Undetermined); @@ -647,9 +640,8 @@ impl<'a> Resolver<'a> { orig_ident: Ident, scope_set: ScopeSet<'a>, parent_scope: &ParentScope<'a>, - record_used: bool, + record_used: Option, force: bool, - path_span: Span, ) -> Result<&'a NameBinding<'a>, Determinacy> { bitflags::bitflags! { struct Flags: u8 { @@ -661,7 +653,7 @@ impl<'a> Resolver<'a> { } } - assert!(force || !record_used); // `record_used` implies `force` + assert!(force || !record_used.is_some()); // `record_used` implies `force` // Make sure `self`, `super` etc produce an error when passed to here. if orig_ident.is_path_segment_keyword() { @@ -769,7 +761,6 @@ impl<'a> Resolver<'a> { ns, parent_scope, record_used, - path_span, ); match binding { Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)), @@ -791,7 +782,6 @@ impl<'a> Resolver<'a> { adjusted_parent_scope, !matches!(scope_set, ScopeSet::Late(..)), record_used, - path_span, ); match binding { Ok(binding) => { @@ -855,12 +845,14 @@ impl<'a> Resolver<'a> { Err(Determinacy::Determined) } } - Scope::ExternPrelude => match this.extern_prelude_get(ident, !record_used) { - Some(binding) => Ok((binding, Flags::empty())), - None => Err(Determinacy::determined( - this.graph_root.unexpanded_invocations.borrow().is_empty(), - )), - }, + Scope::ExternPrelude => { + match this.extern_prelude_get(ident, record_used.is_none()) { + Some(binding) => Ok((binding, Flags::empty())), + None => Err(Determinacy::determined( + this.graph_root.unexpanded_invocations.borrow().is_empty(), + )), + } + } Scope::ToolPrelude => match this.registered_tools.get(&ident).cloned() { Some(ident) => ok(Res::ToolMod, ident.span, this.arenas), None => Err(Determinacy::Determined), @@ -873,8 +865,7 @@ impl<'a> Resolver<'a> { ident, ns, parent_scope, - false, - path_span, + None, ) { if use_prelude || this.is_builtin_macro(binding.res()) { result = Ok((binding, Flags::MISC_FROM_PRELUDE)); @@ -893,7 +884,7 @@ impl<'a> Resolver<'a> { Ok((binding, flags)) if sub_namespace_match(binding.macro_kind(), macro_kind) => { - if !record_used || matches!(scope_set, ScopeSet::Late(..)) { + if record_used.is_none() || matches!(scope_set, ScopeSet::Late(..)) { return Some(Ok(binding)); } @@ -1032,8 +1023,7 @@ impl<'a> Resolver<'a> { &path, Some(MacroNS), &parent_scope, - path_span, - CrateLint::SimplePath(ast::CRATE_NODE_ID), + CrateLint::SimplePath(ast::CRATE_NODE_ID, path_span), ) { PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { let res = path_res.base_res(); @@ -1067,9 +1057,8 @@ impl<'a> Resolver<'a> { ident, ScopeSet::Macro(kind), &parent_scope, + Some(ident.span), true, - true, - ident.span, ) { Ok(binding) => { let initial_res = initial_binding.map(|initial_binding| { @@ -1109,9 +1098,8 @@ impl<'a> Resolver<'a> { ident, ScopeSet::Macro(MacroKind::Attr), &parent_scope, + Some(ident.span), true, - true, - ident.span, ); } } diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index a810a57feb7bb..6d7ca9a94cfbd 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -568,7 +568,7 @@ fn configure_cmake( // We also do this if the user explicitly requested static libstdc++. if builder.config.llvm_static_stdcpp { if !target.contains("msvc") && !target.contains("netbsd") { - if target.contains("apple") { + if target.contains("apple") || target.contains("windows") { ldflags.push_all("-static-libstdc++"); } else { ldflags.push_all("-Wl,-Bsymbolic -static-libstdc++"); From 1ad64a23d4dc99df9cea4b1394d05538f0ccc86a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 24 Mar 2022 02:55:22 +0300 Subject: [PATCH 4/4] resolve: Rename `CrateLint` to `Finalize` And `crate_lint`/`record_used` to `finalize` --- .../rustc_resolve/src/build_reduced_graph.rs | 20 +-- compiler/rustc_resolve/src/diagnostics.rs | 14 +- compiler/rustc_resolve/src/imports.rs | 35 +++-- compiler/rustc_resolve/src/late.rs | 75 ++++------ .../rustc_resolve/src/late/diagnostics.rs | 12 +- compiler/rustc_resolve/src/lib.rs | 141 ++++++++---------- compiler/rustc_resolve/src/macros.rs | 20 +-- 7 files changed, 147 insertions(+), 170 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index c1ddff3d03143..34218e87b51a6 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -9,7 +9,7 @@ use crate::def_collector::collect_definitions; use crate::imports::{Import, ImportKind}; use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef}; use crate::Namespace::{self, MacroNS, TypeNS, ValueNS}; -use crate::{CrateLint, Determinacy, ExternPreludeEntry, Module, ModuleKind, ModuleOrUniformRoot}; +use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError}; use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError}; @@ -235,16 +235,16 @@ impl<'a> AsMut> for BuildReducedGraphVisitor<'a, '_> { impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility { - self.resolve_visibility_speculative(vis, false).unwrap_or_else(|err| { + self.try_resolve_visibility(vis, true).unwrap_or_else(|err| { self.r.report_vis_error(err); ty::Visibility::Public }) } - fn resolve_visibility_speculative<'ast>( + fn try_resolve_visibility<'ast>( &mut self, vis: &'ast ast::Visibility, - speculative: bool, + finalize: bool, ) -> Result> { let parent_scope = &self.parent_scope; match vis.kind { @@ -296,11 +296,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { &segments, Some(TypeNS), parent_scope, - if speculative { CrateLint::No } else { CrateLint::SimplePath(id, path.span) }, + if finalize { Finalize::SimplePath(id, path.span) } else { Finalize::No }, ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => { let res = module.res().expect("visibility resolved to unnamed block"); - if !speculative { + if finalize { self.r.record_partial_res(id, PartialRes::new(res)); } if module.is_normal() { @@ -770,7 +770,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { // correct visibilities for unnamed field placeholders specifically, so the // constructor visibility should still be determined correctly. let field_vis = self - .resolve_visibility_speculative(&field.vis, true) + .try_resolve_visibility(&field.vis, false) .unwrap_or(ty::Visibility::Public); if ctor_vis.is_at_least(field_vis, &*self.r) { ctor_vis = field_vis; @@ -1269,9 +1269,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { let vis = match item.kind { // Visibilities must not be resolved non-speculatively twice // and we already resolved this one as a `fn` item visibility. - ItemKind::Fn(..) => self - .resolve_visibility_speculative(&item.vis, true) - .unwrap_or(ty::Visibility::Public), + ItemKind::Fn(..) => { + self.try_resolve_visibility(&item.vis, false).unwrap_or(ty::Visibility::Public) + } _ => self.resolve_visibility(&item.vis), }; if vis != ty::Visibility::Public { diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index d407a31c0eaf5..d1685006881d5 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -24,10 +24,8 @@ use tracing::debug; use crate::imports::{Import, ImportKind, ImportResolver}; use crate::path_names_to_string; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; -use crate::{ - BindingError, CrateLint, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot, -}; -use crate::{NameBinding, NameBindingKind, PrivacyError, VisResolutionError}; +use crate::{BindingError, HasGenericParams, MacroRulesScope, Module, ModuleOrUniformRoot}; +use crate::{Finalize, NameBinding, NameBindingKind, PrivacyError, VisResolutionError}; use crate::{ParentScope, PathResult, ResolutionError, Resolver, Scope, ScopeSet, Segment}; type Res = def::Res; @@ -1424,7 +1422,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `self` and check if that is valid. path[0].ident.name = kw::SelfLower; - let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No); debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1443,7 +1441,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Crate; - let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No); debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some(( @@ -1474,7 +1472,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { ) -> Option<(Vec, Vec)> { // Replace first ident with `crate` and check if that is valid. path[0].ident.name = kw::Super; - let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No); debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result); if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None } } @@ -1508,7 +1506,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { for name in extern_crate_names.into_iter() { // Replace first ident with a crate name and check if that is valid. path[0].ident.name = name; - let result = self.r.resolve_path(&path, None, parent_scope, CrateLint::No); + let result = self.r.resolve_path(&path, None, parent_scope, Finalize::No); debug!( "make_external_crate_suggestion: name={:?} path={:?} result={:?}", name, path, result diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 34bd39e711e90..4f0dad13b5bbe 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -6,7 +6,7 @@ use crate::Namespace::{self, MacroNS, TypeNS}; use crate::{module_to_string, names_to_string}; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; use crate::{BindingKey, ModuleKind, ResolutionError, Resolver, Segment}; -use crate::{CrateLint, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet, Weak}; +use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet, Weak}; use crate::{NameBinding, NameBindingKind, PathResult, PrivacyError, ToNameBinding}; use rustc_ast::NodeId; @@ -175,7 +175,7 @@ impl<'a> Resolver<'a> { ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: Option, + finalize: Option, ) -> Result<&'a NameBinding<'a>, Determinacy> { self.resolve_ident_in_module_unadjusted_ext( module, @@ -183,13 +183,13 @@ impl<'a> Resolver<'a> { ns, parent_scope, false, - record_used, + finalize, ) .map_err(|(determinacy, _)| determinacy) } /// Attempts to resolve `ident` in namespaces `ns` of `module`. - /// Invariant: if `record_used` is `Some`, expansion and import resolution must be complete. + /// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete. crate fn resolve_ident_in_module_unadjusted_ext( &mut self, module: ModuleOrUniformRoot<'a>, @@ -197,7 +197,7 @@ impl<'a> Resolver<'a> { ns: Namespace, parent_scope: &ParentScope<'a>, restricted_shadowing: bool, - record_used: Option, + finalize: Option, ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> { let module = match module { ModuleOrUniformRoot::Module(module) => module, @@ -207,8 +207,8 @@ impl<'a> Resolver<'a> { ident, ScopeSet::AbsolutePath(ns), parent_scope, - record_used, - record_used.is_some(), + finalize, + finalize.is_some(), ); return binding.map_err(|determinacy| (determinacy, Weak::No)); } @@ -216,8 +216,7 @@ impl<'a> Resolver<'a> { assert!(!restricted_shadowing); return if ns != TypeNS { Err((Determined, Weak::No)) - } else if let Some(binding) = self.extern_prelude_get(ident, record_used.is_none()) - { + } else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) { Ok(binding) } else if !self.graph_root.unexpanded_invocations.borrow().is_empty() { // Macro-expanded `extern crate` items can add names to extern prelude. @@ -247,8 +246,8 @@ impl<'a> Resolver<'a> { ident, scopes, parent_scope, - record_used, - record_used.is_some(), + finalize, + finalize.is_some(), ); return binding.map_err(|determinacy| (determinacy, Weak::No)); } @@ -258,7 +257,7 @@ impl<'a> Resolver<'a> { let resolution = self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports. - if let Some(binding) = resolution.binding && let Some(path_span) = record_used { + if let Some(binding) = resolution.binding && let Some(path_span) = finalize { if !restricted_shadowing && binding.expansion != LocalExpnId::ROOT { if let NameBindingKind::Res(_, true) = binding.kind { self.macro_expanded_macro_export_errors.insert((path_span, binding.span)); @@ -276,7 +275,7 @@ impl<'a> Resolver<'a> { if usable { Ok(binding) } else { Err((Determined, Weak::No)) } }; - if let Some(path_span) = record_used { + if let Some(path_span) = finalize { return resolution .binding .and_then(|binding| { @@ -773,7 +772,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { // not define any names while resolving its module path. let orig_vis = import.vis.replace(ty::Visibility::Invisible); let path_res = - self.r.resolve_path(&import.module_path, None, &import.parent_scope, CrateLint::No); + self.r.resolve_path(&import.module_path, None, &import.parent_scope, Finalize::No); import.vis.set(orig_vis); match path_res { @@ -865,13 +864,13 @@ impl<'a, 'b> ImportResolver<'a, 'b> { _ => None, }; let prev_ambiguity_errors_len = self.r.ambiguity_errors.len(); - let crate_lint = CrateLint::UsePath { + let finalize = Finalize::UsePath { root_id: import.root_id, root_span: import.root_span, path_span: import.span, }; let path_res = - self.r.resolve_path(&import.module_path, None, &import.parent_scope, crate_lint); + self.r.resolve_path(&import.module_path, None, &import.parent_scope, finalize); let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len; if let Some(orig_unusable_binding) = orig_unusable_binding { self.r.unusable_binding = orig_unusable_binding; @@ -958,7 +957,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { // 2 segments, so the `resolve_path` above won't trigger it. let mut full_path = import.module_path.clone(); full_path.push(Segment::from_ident(Ident::empty())); - self.r.lint_if_path_starts_with_module(crate_lint, &full_path, None); + self.r.lint_if_path_starts_with_module(finalize, &full_path, None); } if let ModuleOrUniformRoot::Module(module) = module { @@ -1223,7 +1222,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { full_path.push(Segment::from_ident(ident)); self.r.per_ns(|this, ns| { if let Ok(binding) = source_bindings[ns].get() { - this.lint_if_path_starts_with_module(crate_lint, &full_path, Some(binding)); + this.lint_if_path_starts_with_module(finalize, &full_path, Some(binding)); } }); } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 995a48be407b1..bb05a3d7510e1 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -7,7 +7,7 @@ use RibKind::*; -use crate::{path_names_to_string, BindingError, CrateLint, LexicalScopeBinding}; +use crate::{path_names_to_string, BindingError, Finalize, LexicalScopeBinding}; use crate::{Module, ModuleOrUniformRoot, ParentScope, PathResult}; use crate::{ResolutionError, Resolver, Segment, UseError}; @@ -486,7 +486,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { .resolve_ident_in_lexical_scope( self_ty, TypeNS, - CrateLint::SimplePath(ty.id, ty.span), + Finalize::SimplePath(ty.id, ty.span), ) .map_or(Res::Err, |d| d.res()); self.r.record_partial_res(ty.id, PartialRes::new(res)); @@ -679,7 +679,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> { self.resolve_ident_in_lexical_scope( path.segments[0].ident, ns, - CrateLint::No, + Finalize::No, ) .is_some() }; @@ -754,13 +754,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { &mut self, ident: Ident, ns: Namespace, - crate_lint: CrateLint, + finalize: Finalize, ) -> Option> { self.r.resolve_ident_in_lexical_scope( ident, ns, &self.parent_scope, - crate_lint, + finalize, &self.ribs[ns], ) } @@ -769,15 +769,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { &mut self, path: &[Segment], opt_ns: Option, // `None` indicates a module path in import - crate_lint: CrateLint, + finalize: Finalize, ) -> PathResult<'a> { - self.r.resolve_path_with_ribs( - path, - opt_ns, - &self.parent_scope, - crate_lint, - Some(&self.ribs), - ) + self.r.resolve_path_with_ribs(path, opt_ns, &self.parent_scope, finalize, Some(&self.ribs)) } // AST resolution @@ -940,7 +934,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { }; for &ns in nss { - match self.resolve_ident_in_lexical_scope(ident, ns, CrateLint::No) { + match self.resolve_ident_in_lexical_scope(ident, ns, Finalize::No) { Some(LexicalScopeBinding::Res(..)) => { report_error(self, ns); } @@ -948,7 +942,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { let orig_unusable_binding = replace(&mut self.r.unusable_binding, Some(binding)); if let Some(LexicalScopeBinding::Res(..)) = - self.resolve_ident_in_lexical_scope(ident, ns, CrateLint::No) + self.resolve_ident_in_lexical_scope(ident, ns, Finalize::No) { report_error(self, ns); } @@ -1246,7 +1240,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { None, &path, PathSource::Trait(AliasPossibility::No), - CrateLint::SimplePath(trait_ref.ref_id, trait_ref.path.span), + Finalize::SimplePath(trait_ref.ref_id, trait_ref.path.span), ); if let Some(def_id) = res.base_res().opt_def_id() { new_id = Some(def_id); @@ -1808,7 +1802,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // also be interpreted as a path to e.g. a constant, variant, etc. let is_syntactic_ambiguity = !has_sub && bm == BindingMode::ByValue(Mutability::Not); - let ls_binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, CrateLint::No)?; + let ls_binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, Finalize::No)?; let (res, binding) = match ls_binding { LexicalScopeBinding::Item(binding) if is_syntactic_ambiguity && binding.is_ambiguity() => @@ -1900,7 +1894,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { qself, &Segment::from_path(path), source, - CrateLint::SimplePath(id, path.span), + Finalize::SimplePath(id, path.span), ); } @@ -1909,18 +1903,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { qself: Option<&QSelf>, path: &[Segment], source: PathSource<'ast>, - crate_lint: CrateLint, + finalize: Finalize, ) -> PartialRes { tracing::debug!( - "smart_resolve_path_fragment(qself={:?}, path={:?}, crate_lint={:?})", + "smart_resolve_path_fragment(qself={:?}, path={:?}, finalize={:?})", qself, path, - crate_lint, + finalize, ); let ns = source.namespace(); let (id, path_span) = - crate_lint.node_id_and_path_span().expect("unexpected speculative resolution"); + finalize.node_id_and_path_span().expect("unexpected speculative resolution"); let report_errors = |this: &mut Self, res: Option| { if this.should_report_errs() { let (err, candidates) = @@ -2016,7 +2010,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ns, path_span, source.defer_to_typeck(), - crate_lint, + finalize, ) { Ok(Some(partial_res)) if partial_res.unresolved_segments() == 0 => { if source.is_expected(partial_res.base_res()) || partial_res.base_res() == Res::Err @@ -2043,7 +2037,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std))); std_path.extend(path); if let PathResult::Module(_) | PathResult::NonModule(_) = - self.resolve_path(&std_path, Some(ns), CrateLint::No) + self.resolve_path(&std_path, Some(ns), Finalize::No) { // Check if we wrote `str::from_utf8` instead of `std::str::from_utf8` let item_span = @@ -2080,14 +2074,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { let binding = self.resolve_ident_in_lexical_scope( Ident::with_dummy_span(kw::SelfUpper), TypeNS, - CrateLint::No, + Finalize::No, ); if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false } } fn self_value_is_available(&mut self, self_span: Span) -> bool { let ident = Ident::new(kw::SelfLower, self_span); - let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, CrateLint::No); + let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, Finalize::No); if let Some(LexicalScopeBinding::Res(res)) = binding { res != Res::Err } else { false } } @@ -2114,13 +2108,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { primary_ns: Namespace, span: Span, defer_to_typeck: bool, - crate_lint: CrateLint, + finalize: Finalize, ) -> Result, Spanned>> { let mut fin_res = None; for (i, &ns) in [primary_ns, TypeNS, ValueNS].iter().enumerate() { if i == 0 || ns != primary_ns { - match self.resolve_qpath(qself, path, ns, crate_lint)? { + match self.resolve_qpath(qself, path, ns, finalize)? { Some(partial_res) if partial_res.unresolved_segments() == 0 || defer_to_typeck => { @@ -2156,11 +2150,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { qself: Option<&QSelf>, path: &[Segment], ns: Namespace, - crate_lint: CrateLint, + finalize: Finalize, ) -> Result, Spanned>> { debug!( - "resolve_qpath(qself={:?}, path={:?}, ns={:?}, crate_lint={:?})", - qself, path, ns, crate_lint, + "resolve_qpath(qself={:?}, path={:?}, ns={:?}, finalize={:?})", + qself, path, ns, finalize, ); if let Some(qself) = qself { @@ -2187,20 +2181,15 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { // *actually* appears, so for the purposes of the crate // lint we pass along information that this is the trait // name from a fully qualified path, and this also - // contains the full span (the `CrateLint::QPathTrait`). + // contains the full span (the `Finalize::QPathTrait`). let ns = if qself.position + 1 == path.len() { ns } else { TypeNS }; let partial_res = self.smart_resolve_path_fragment( None, &path[..=qself.position], PathSource::TraitItem(ns), - crate_lint.node_id_and_path_span().map_or( - CrateLint::No, - |(qpath_id, path_span)| CrateLint::QPathTrait { - qpath_id, - qpath_span: qself.path_span, - path_span, - }, - ), + finalize.node_id_and_path_span().map_or(Finalize::No, |(qpath_id, path_span)| { + Finalize::QPathTrait { qpath_id, qpath_span: qself.path_span, path_span } + }), ); // The remaining segments (the `C` in our example) will @@ -2212,7 +2201,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { ))); } - let result = match self.resolve_path(&path, Some(ns), crate_lint) { + let result = match self.resolve_path(&path, Some(ns), finalize) { PathResult::NonModule(path_res) => path_res, PathResult::Module(ModuleOrUniformRoot::Module(module)) if !module.is_normal() => { PartialRes::new(module.res().unwrap()) @@ -2250,10 +2239,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { && result.base_res() != Res::Err && path[0].ident.name != kw::PathRoot && path[0].ident.name != kw::DollarCrate - && let Some((id, path_span)) = crate_lint.node_id_and_path_span() + && let Some((id, path_span)) = finalize.node_id_and_path_span() { let unqualified_result = { - match self.resolve_path(&[*path.last().unwrap()], Some(ns), CrateLint::No) { + match self.resolve_path(&[*path.last().unwrap()], Some(ns), Finalize::No) { PathResult::NonModule(path_res) => path_res.base_res(), PathResult::Module(ModuleOrUniformRoot::Module(module)) => { module.res().unwrap() diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index fc04ce6dfb0a5..038ba2206086e 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2,7 +2,7 @@ use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion}; use crate::late::lifetimes::{ElisionFailureInfo, LifetimeContext}; use crate::late::{AliasPossibility, LateResolutionVisitor, RibKind}; use crate::path_names_to_string; -use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot}; +use crate::{Finalize, Module, ModuleKind, ModuleOrUniformRoot}; use crate::{PathResult, PathSource, Segment}; use rustc_ast::visit::FnKind; @@ -187,7 +187,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { (String::new(), "the crate root".to_string()) } else { let mod_path = &path[..path.len() - 1]; - let mod_prefix = match self.resolve_path(mod_path, Some(TypeNS), CrateLint::No) { + let mod_prefix = match self.resolve_path(mod_path, Some(TypeNS), Finalize::No) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => module.res(), _ => None, } @@ -646,7 +646,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { if let crate::PathSource::TraitItem(_) = source { let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, None, CrateLint::No) + self.resolve_path(mod_path, None, Finalize::No) { let resolutions = self.r.resolutions(module).borrow(); let targets: Vec<_> = @@ -702,7 +702,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { Namespace::TypeNS, span, true, - CrateLint::No, + Finalize::No, ) else { return false; }; @@ -726,7 +726,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { Namespace::TypeNS, span, true, - CrateLint::No, + Finalize::No, ) else { return false; }; @@ -1378,7 +1378,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { // Search in module. let mod_path = &path[..path.len() - 1]; if let PathResult::Module(ModuleOrUniformRoot::Module(module)) = - self.resolve_path(mod_path, Some(TypeNS), CrateLint::No) + self.resolve_path(mod_path, Some(TypeNS), Finalize::No) { self.r.add_module_candidates(module, &mut names, &filter_fn); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index a4fe7676fc459..19eeae4cf233b 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -454,11 +454,11 @@ impl<'a> PathResult<'a> { fn failed( span: Span, is_error_from_last_segment: bool, - record_used: bool, + finalize: bool, label_and_suggestion: impl FnOnce() -> (String, Option), ) -> PathResult<'a> { let (label, suggestion) = - if record_used { label_and_suggestion() } else { (String::new(), None) }; + if finalize { label_and_suggestion() } else { (String::new(), None) }; PathResult::Failed { span, label, suggestion, is_error_from_last_segment } } } @@ -1950,7 +1950,7 @@ impl<'a> Resolver<'a> { mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - crate_lint: CrateLint, + finalize_full: Finalize, ribs: &[Rib<'a>], ) -> Option> { assert!(ns == TypeNS || ns == ValueNS); @@ -1972,7 +1972,7 @@ impl<'a> Resolver<'a> { let normalized_ident = Ident { span: normalized_span, ..ident }; // Walk backwards up the ribs in scope. - let record_used = crate_lint.path_span(); + let finalize = finalize_full.path_span(); let mut module = self.graph_root; for i in (0..ribs.len()).rev() { debug!("walk rib\n{:?}", ribs[i].bindings); @@ -1986,7 +1986,7 @@ impl<'a> Resolver<'a> { i, rib_ident, *res, - record_used, + finalize, *original_rib_ident_def, ribs, ))); @@ -2013,7 +2013,7 @@ impl<'a> Resolver<'a> { ident, ns, parent_scope, - record_used, + finalize, ); if let Ok(binding) = item { // The ident resolves to an item. @@ -2022,10 +2022,10 @@ impl<'a> Resolver<'a> { } self.early_resolve_ident_in_lexical_scope( orig_ident, - ScopeSet::Late(ns, module, crate_lint.node_id()), + ScopeSet::Late(ns, module, finalize_full.node_id()), parent_scope, - record_used, - record_used.is_some(), + finalize, + finalize.is_some(), ) .ok() .map(LexicalScopeBinding::Item) @@ -2085,9 +2085,9 @@ impl<'a> Resolver<'a> { ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: Option, + finalize: Option, ) -> Result<&'a NameBinding<'a>, Determinacy> { - self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, record_used) + self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, finalize) .map_err(|(determinacy, _)| determinacy) } @@ -2097,7 +2097,7 @@ impl<'a> Resolver<'a> { mut ident: Ident, ns: Namespace, parent_scope: &ParentScope<'a>, - record_used: Option, + finalize: Option, ) -> Result<&'a NameBinding<'a>, (Determinacy, Weak)> { let tmp_parent_scope; let mut adjusted_parent_scope = parent_scope; @@ -2122,7 +2122,7 @@ impl<'a> Resolver<'a> { ns, adjusted_parent_scope, false, - record_used, + finalize, ) } @@ -2213,9 +2213,9 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - crate_lint: CrateLint, + finalize: Finalize, ) -> PathResult<'a> { - self.resolve_path_with_ribs(path, opt_ns, parent_scope, crate_lint, None) + self.resolve_path_with_ribs(path, opt_ns, parent_scope, finalize, None) } fn resolve_path_with_ribs( @@ -2223,12 +2223,12 @@ impl<'a> Resolver<'a> { path: &[Segment], opt_ns: Option, // `None` indicates a module path in import parent_scope: &ParentScope<'a>, - crate_lint: CrateLint, + finalize_full: Finalize, ribs: Option<&PerNS>>>, ) -> PathResult<'a> { - debug!("resolve_path(path={:?}, opt_ns={:?}, crate_lint={:?})", path, opt_ns, crate_lint); + debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize_full); - let record_used = crate_lint.path_span(); + let finalize = finalize_full.path_span(); let mut module = None; let mut allow_super = true; let mut second_binding = None; @@ -2236,7 +2236,7 @@ impl<'a> Resolver<'a> { for (i, &Segment { ident, id, has_generic_args: _ }) in path.iter().enumerate() { debug!("resolve_path ident {} {:?} {:?}", i, ident, id); let record_segment_res = |this: &mut Self, res| { - if record_used.is_some() { + if finalize.is_some() { if let Some(id) = id { if !this.partial_res_map.contains_key(&id) { assert!(id != ast::DUMMY_NODE_ID, "Trying to resolve dummy id"); @@ -2270,7 +2270,7 @@ impl<'a> Resolver<'a> { continue; } } - return PathResult::failed(ident.span, false, record_used.is_some(), || { + return PathResult::failed(ident.span, false, finalize.is_some(), || { ("there are too many leading `super` keywords".to_string(), None) }); } @@ -2301,7 +2301,7 @@ impl<'a> Resolver<'a> { // Report special messages for path segment keywords in wrong positions. if ident.is_path_segment_keyword() && i != 0 { - return PathResult::failed(ident.span, false, record_used.is_some(), || { + return PathResult::failed(ident.span, false, finalize.is_some(), || { let name_str = if name == kw::PathRoot { "crate root".to_string() } else { @@ -2322,22 +2322,22 @@ impl<'a> Resolver<'a> { } let find_binding_in_ns = |this: &mut Self, ns| { let binding = if let Some(module) = module { - this.resolve_ident_in_module(module, ident, ns, parent_scope, record_used) + this.resolve_ident_in_module(module, ident, ns, parent_scope, finalize) } else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) { let scopes = ScopeSet::All(ns, opt_ns.is_none()); this.early_resolve_ident_in_lexical_scope( ident, scopes, parent_scope, - record_used, - record_used.is_some(), + finalize, + finalize.is_some(), ) } else { match this.resolve_ident_in_lexical_scope( ident, ns, parent_scope, - crate_lint, + finalize_full, &ribs.unwrap()[ns], ) { // we found a locally-imported or available item/module @@ -2351,7 +2351,7 @@ impl<'a> Resolver<'a> { PartialRes::with_unresolved_segments(res, path.len() - 1), )); } - _ => Err(Determinacy::determined(record_used.is_some())), + _ => Err(Determinacy::determined(finalize.is_some())), } }; FindBindingResult::Binding(binding) @@ -2385,25 +2385,20 @@ impl<'a> Resolver<'a> { } else if res == Res::Err { return PathResult::NonModule(PartialRes::new(Res::Err)); } else if opt_ns.is_some() && (is_last || maybe_assoc) { - self.lint_if_path_starts_with_module(crate_lint, path, second_binding); + self.lint_if_path_starts_with_module(finalize_full, path, second_binding); return PathResult::NonModule(PartialRes::with_unresolved_segments( res, path.len() - i - 1, )); } else { - return PathResult::failed( - ident.span, - is_last, - record_used.is_some(), - || { - let label = format!( - "`{ident}` is {} {}, not a module", - res.article(), - res.descr() - ); - (label, None) - }, - ); + return PathResult::failed(ident.span, is_last, finalize.is_some(), || { + let label = format!( + "`{ident}` is {} {}, not a module", + res.article(), + res.descr() + ); + (label, None) + }); } } Err(Undetermined) => return PathResult::Indeterminate, @@ -2417,7 +2412,7 @@ impl<'a> Resolver<'a> { } } - return PathResult::failed(ident.span, is_last, record_used.is_some(), || { + return PathResult::failed(ident.span, is_last, finalize.is_some(), || { let module_res = match module { Some(ModuleOrUniformRoot::Module(module)) => module.res(), _ => None, @@ -2457,7 +2452,7 @@ impl<'a> Resolver<'a> { ident, ValueNS, parent_scope, - CrateLint::No, + Finalize::No, &ribs.unwrap()[ValueNS], ) { // Name matches a local variable. For example: @@ -2582,7 +2577,7 @@ impl<'a> Resolver<'a> { } } - self.lint_if_path_starts_with_module(crate_lint, path, second_binding); + self.lint_if_path_starts_with_module(finalize_full, path, second_binding); PathResult::Module(match module { Some(module) => module, @@ -2593,15 +2588,15 @@ impl<'a> Resolver<'a> { fn lint_if_path_starts_with_module( &mut self, - crate_lint: CrateLint, + finalize: Finalize, path: &[Segment], second_binding: Option<&NameBinding<'_>>, ) { - let (diag_id, diag_span) = match crate_lint { - CrateLint::No => return, - CrateLint::SimplePath(id, path_span) => (id, path_span), - CrateLint::UsePath { root_id, root_span, .. } => (root_id, root_span), - CrateLint::QPathTrait { qpath_id, qpath_span, .. } => (qpath_id, qpath_span), + let (diag_id, diag_span) = match finalize { + Finalize::No => return, + Finalize::SimplePath(id, path_span) => (id, path_span), + Finalize::UsePath { root_id, root_span, .. } => (root_id, root_span), + Finalize::QPathTrait { qpath_id, qpath_span, .. } => (qpath_id, qpath_span), }; let first_name = match path.get(0) { @@ -2656,7 +2651,7 @@ impl<'a> Resolver<'a> { rib_index: usize, rib_ident: Ident, mut res: Res, - record_used: Option, + finalize: Option, original_rib_ident_def: Ident, all_ribs: &[Rib<'a>], ) -> Res { @@ -2666,7 +2661,7 @@ impl<'a> Resolver<'a> { // An invalid forward use of a generic parameter from a previous default. if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind { - if let Some(span) = record_used { + if let Some(span) = finalize { let res_error = if rib_ident.name == kw::SelfUpper { ResolutionError::SelfInGenericParamDefault } else { @@ -2696,7 +2691,7 @@ impl<'a> Resolver<'a> { // This was an attempt to access an upvar inside a // named function item. This is not allowed, so we // report an error. - if let Some(span) = record_used { + if let Some(span) = finalize { // We don't immediately trigger a resolve error, because // we want certain other resolution errors (namely those // emitted for `ConstantItemRibKind` below) to take @@ -2706,7 +2701,7 @@ impl<'a> Resolver<'a> { } ConstantItemRibKind(_, item) => { // Still doesn't deal with upvars - if let Some(span) = record_used { + if let Some(span) = finalize { let (span, resolution_error) = if let Some((ident, constant_item_kind)) = item { let kind_str = match constant_item_kind { @@ -2734,7 +2729,7 @@ impl<'a> Resolver<'a> { return Res::Err; } ConstParamTyRibKind => { - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error(span, ParamInTyOfConstParam(rib_ident.name)); } return Res::Err; @@ -2769,7 +2764,7 @@ impl<'a> Resolver<'a> { if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res { res = Res::SelfTy { trait_, alias_to: Some((def, true)) } } else { - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::ParamInNonTrivialAnonConst { @@ -2791,7 +2786,7 @@ impl<'a> Resolver<'a> { ItemRibKind(has_generic_params) => has_generic_params, FnItemRibKind => HasGenericParams::Yes, ConstParamTyRibKind => { - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::ParamInTyOfConstParam(rib_ident.name), @@ -2801,7 +2796,7 @@ impl<'a> Resolver<'a> { } }; - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::GenericParamsFromOuterFunction( @@ -2835,7 +2830,7 @@ impl<'a> Resolver<'a> { let features = self.session.features_untracked(); // HACK(min_const_generics): We currently only allow `N` or `{ N }`. if !(trivial || features.generic_const_exprs) { - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::ParamInNonTrivialAnonConst { @@ -2855,7 +2850,7 @@ impl<'a> Resolver<'a> { ItemRibKind(has_generic_params) => has_generic_params, FnItemRibKind => HasGenericParams::Yes, ConstParamTyRibKind => { - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::ParamInTyOfConstParam(rib_ident.name), @@ -2866,7 +2861,7 @@ impl<'a> Resolver<'a> { }; // This was an attempt to use a const parameter outside its scope. - if let Some(span) = record_used { + if let Some(span) = finalize { self.report_error( span, ResolutionError::GenericParamsFromOuterFunction( @@ -3254,23 +3249,19 @@ impl<'a> Resolver<'a> { err.span_suggestion(span, message, String::new(), Applicability::MachineApplicable); } - fn extern_prelude_get( - &mut self, - ident: Ident, - speculative: bool, - ) -> Option<&'a NameBinding<'a>> { + fn extern_prelude_get(&mut self, ident: Ident, finalize: bool) -> Option<&'a NameBinding<'a>> { if ident.is_path_segment_keyword() { // Make sure `self`, `super` etc produce an error when passed to here. return None; } self.extern_prelude.get(&ident.normalize_to_macros_2_0()).cloned().and_then(|entry| { if let Some(binding) = entry.extern_crate_item { - if !speculative && entry.introduced_by_item { + if finalize && entry.introduced_by_item { self.record_use(ident, binding, false); } Some(binding) } else { - let crate_id = if !speculative { + let crate_id = if finalize { let Some(crate_id) = self.crate_loader.process_path_extern(ident.name, ident.span) else { return Some(self.dummy_binding); }; crate_id @@ -3307,7 +3298,7 @@ impl<'a> Resolver<'a> { &segments, Some(ns), &ParentScope::module(module, self), - CrateLint::No, + Finalize::No, ) { PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()), PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { @@ -3458,8 +3449,8 @@ fn module_to_string(module: Module<'_>) -> Option { Some(names_to_string(&names)) } -#[derive(Copy, Clone, PartialEq, Debug)] -enum CrateLint { +#[derive(Copy, Clone, Debug)] +enum Finalize { /// Do not issue the lint. No, @@ -3479,13 +3470,13 @@ enum CrateLint { QPathTrait { qpath_id: NodeId, qpath_span: Span, path_span: Span }, } -impl CrateLint { +impl Finalize { fn node_id_and_path_span(&self) -> Option<(NodeId, Span)> { match *self { - CrateLint::No => None, - CrateLint::SimplePath(id, path_span) - | CrateLint::UsePath { root_id: id, path_span, .. } - | CrateLint::QPathTrait { qpath_id: id, path_span, .. } => Some((id, path_span)), + Finalize::No => None, + Finalize::SimplePath(id, path_span) + | Finalize::UsePath { root_id: id, path_span, .. } + | Finalize::QPathTrait { qpath_id: id, path_span, .. } => Some((id, path_span)), } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index e8ba8780526c5..dc94ba49a5400 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -4,7 +4,7 @@ use crate::imports::ImportResolver; use crate::Namespace::*; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BuiltinMacroState, Determinacy}; -use crate::{CrateLint, DeriveData, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak}; +use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, Scope, ScopeSet, Weak}; use crate::{ModuleKind, ModuleOrUniformRoot, NameBinding, PathResult, Segment, ToNameBinding}; use rustc_ast::{self as ast, Inline, ItemKind, ModKind, NodeId}; use rustc_ast_lowering::ResolverAstLowering; @@ -415,7 +415,7 @@ impl<'a> ResolverExpand for Resolver<'a> { let mut indeterminate = false; for ns in [TypeNS, ValueNS, MacroNS].iter().copied() { - match self.resolve_path(path, Some(ns), &parent_scope, CrateLint::No) { + match self.resolve_path(path, Some(ns), &parent_scope, Finalize::No) { PathResult::Module(ModuleOrUniformRoot::Module(_)) => return Ok(true), PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => { return Ok(true); @@ -575,7 +575,7 @@ impl<'a> Resolver<'a> { } let res = if path.len() > 1 { - let res = match self.resolve_path(&path, Some(MacroNS), parent_scope, CrateLint::No) { + let res = match self.resolve_path(&path, Some(MacroNS), parent_scope, Finalize::No) { PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { Ok(path_res.base_res()) } @@ -640,7 +640,7 @@ impl<'a> Resolver<'a> { orig_ident: Ident, scope_set: ScopeSet<'a>, parent_scope: &ParentScope<'a>, - record_used: Option, + finalize: Option, force: bool, ) -> Result<&'a NameBinding<'a>, Determinacy> { bitflags::bitflags! { @@ -653,7 +653,7 @@ impl<'a> Resolver<'a> { } } - assert!(force || !record_used.is_some()); // `record_used` implies `force` + assert!(force || !finalize.is_some()); // `finalize` implies `force` // Make sure `self`, `super` etc produce an error when passed to here. if orig_ident.is_path_segment_keyword() { @@ -760,7 +760,7 @@ impl<'a> Resolver<'a> { ident, ns, parent_scope, - record_used, + finalize, ); match binding { Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)), @@ -781,7 +781,7 @@ impl<'a> Resolver<'a> { ns, adjusted_parent_scope, !matches!(scope_set, ScopeSet::Late(..)), - record_used, + finalize, ); match binding { Ok(binding) => { @@ -846,7 +846,7 @@ impl<'a> Resolver<'a> { } } Scope::ExternPrelude => { - match this.extern_prelude_get(ident, record_used.is_none()) { + match this.extern_prelude_get(ident, finalize.is_some()) { Some(binding) => Ok((binding, Flags::empty())), None => Err(Determinacy::determined( this.graph_root.unexpanded_invocations.borrow().is_empty(), @@ -884,7 +884,7 @@ impl<'a> Resolver<'a> { Ok((binding, flags)) if sub_namespace_match(binding.macro_kind(), macro_kind) => { - if record_used.is_none() || matches!(scope_set, ScopeSet::Late(..)) { + if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) { return Some(Ok(binding)); } @@ -1023,7 +1023,7 @@ impl<'a> Resolver<'a> { &path, Some(MacroNS), &parent_scope, - CrateLint::SimplePath(ast::CRATE_NODE_ID, path_span), + Finalize::SimplePath(ast::CRATE_NODE_ID, path_span), ) { PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => { let res = path_res.base_res();