diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index c1ddff3d031..34218e87b51 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 d407a31c0ea..d1685006881 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 34bd39e711e..4f0dad13b5b 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 995a48be407..bb05a3d7510 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 fc04ce6dfb0..038ba220608 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 a4fe7676fc4..19eeae4cf23 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 e8ba8780526..dc94ba49a54 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();