summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_resolve
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:11:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:12:43 +0000
commitcf94bdc0742c13e2a0cac864c478b8626b266e1b (patch)
tree044670aa50cc5e2b4229aa0b6b3df6676730c0a6 /compiler/rustc_resolve
parentAdding debian version 1.65.0+dfsg1-2. (diff)
downloadrustc-cf94bdc0742c13e2a0cac864c478b8626b266e1b.tar.xz
rustc-cf94bdc0742c13e2a0cac864c478b8626b266e1b.zip
Merging upstream version 1.66.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_resolve')
-rw-r--r--compiler/rustc_resolve/Cargo.toml1
-rw-r--r--compiler/rustc_resolve/src/access_levels.rs185
-rw-r--r--compiler/rustc_resolve/src/build_reduced_graph.rs7
-rw-r--r--compiler/rustc_resolve/src/check_unused.rs2
-rw-r--r--compiler/rustc_resolve/src/def_collector.rs27
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs139
-rw-r--r--compiler/rustc_resolve/src/effective_visibilities.rs188
-rw-r--r--compiler/rustc_resolve/src/ident.rs22
-rw-r--r--compiler/rustc_resolve/src/imports.rs32
-rw-r--r--compiler/rustc_resolve/src/late.rs366
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs859
-rw-r--r--compiler/rustc_resolve/src/late/lifetimes.rs1855
-rw-r--r--compiler/rustc_resolve/src/lib.rs82
-rw-r--r--compiler/rustc_resolve/src/macros.rs25
14 files changed, 1120 insertions, 2670 deletions
diff --git a/compiler/rustc_resolve/Cargo.toml b/compiler/rustc_resolve/Cargo.toml
index 5d2b606b4..d66db1d7a 100644
--- a/compiler/rustc_resolve/Cargo.toml
+++ b/compiler/rustc_resolve/Cargo.toml
@@ -4,7 +4,6 @@ version = "0.0.0"
edition = "2021"
[lib]
-doctest = false
[dependencies]
bitflags = "1.2.1"
diff --git a/compiler/rustc_resolve/src/access_levels.rs b/compiler/rustc_resolve/src/access_levels.rs
deleted file mode 100644
index 0a3add2e0..000000000
--- a/compiler/rustc_resolve/src/access_levels.rs
+++ /dev/null
@@ -1,185 +0,0 @@
-use crate::imports::ImportKind;
-use crate::NameBinding;
-use crate::NameBindingKind;
-use crate::Resolver;
-use rustc_ast::ast;
-use rustc_ast::visit;
-use rustc_ast::visit::Visitor;
-use rustc_ast::Crate;
-use rustc_ast::EnumDef;
-use rustc_ast::NodeId;
-use rustc_hir::def_id::LocalDefId;
-use rustc_hir::def_id::CRATE_DEF_ID;
-use rustc_middle::middle::privacy::AccessLevel;
-use rustc_middle::ty::DefIdTree;
-use rustc_span::sym;
-
-pub struct AccessLevelsVisitor<'r, 'a> {
- r: &'r mut Resolver<'a>,
- changed: bool,
-}
-
-impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
- /// Fills the `Resolver::access_levels` table with public & exported items
- /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
- /// need access to a TyCtxt for that.
- pub fn compute_access_levels<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
- let mut visitor = AccessLevelsVisitor { r, changed: false };
-
- visitor.set_access_level_def_id(CRATE_DEF_ID, Some(AccessLevel::Public));
- visitor.set_bindings_access_level(CRATE_DEF_ID);
-
- while visitor.changed {
- visitor.reset();
- visit::walk_crate(&mut visitor, krate);
- }
-
- info!("resolve::access_levels: {:#?}", r.access_levels);
- }
-
- fn reset(&mut self) {
- self.changed = false;
- }
-
- /// Update the access level of the bindings in the given module accordingly. The module access
- /// level has to be Exported or Public.
- /// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
- fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
- assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
- let module_level = self.r.access_levels.map.get(&module_id).copied();
- if !module_level.is_some() {
- return;
- }
- // Set the given binding access level to `AccessLevel::Public` and
- // sets the rest of the `use` chain to `AccessLevel::Exported` until
- // we hit the actual exported item.
- let set_import_binding_access_level =
- |this: &mut Self, mut binding: &NameBinding<'a>, mut access_level| {
- while let NameBindingKind::Import { binding: nested_binding, import, .. } =
- binding.kind
- {
- this.set_access_level(import.id, access_level);
- if let ImportKind::Single { additional_ids, .. } = import.kind {
- this.set_access_level(additional_ids.0, access_level);
- this.set_access_level(additional_ids.1, access_level);
- }
-
- access_level = Some(AccessLevel::Exported);
- binding = nested_binding;
- }
- };
-
- let module = self.r.get_module(module_id.to_def_id()).unwrap();
- let resolutions = self.r.resolutions(module);
-
- for (.., name_resolution) in resolutions.borrow().iter() {
- if let Some(binding) = name_resolution.borrow().binding() && binding.vis.is_public() && !binding.is_ambiguity() {
- let access_level = match binding.is_import() {
- true => {
- set_import_binding_access_level(self, binding, module_level);
- Some(AccessLevel::Exported)
- },
- false => module_level,
- };
- if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
- self.set_access_level_def_id(def_id, access_level);
- }
- }
- }
- }
-
- /// Sets the access level of the `LocalDefId` corresponding to the given `NodeId`.
- /// This function will panic if the `NodeId` does not have a `LocalDefId`
- fn set_access_level(
- &mut self,
- node_id: NodeId,
- access_level: Option<AccessLevel>,
- ) -> Option<AccessLevel> {
- self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
- }
-
- fn set_access_level_def_id(
- &mut self,
- def_id: LocalDefId,
- access_level: Option<AccessLevel>,
- ) -> Option<AccessLevel> {
- let old_level = self.r.access_levels.map.get(&def_id).copied();
- if old_level < access_level {
- self.r.access_levels.map.insert(def_id, access_level.unwrap());
- self.changed = true;
- access_level
- } else {
- old_level
- }
- }
-}
-
-impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
- fn visit_item(&mut self, item: &'ast ast::Item) {
- let def_id = self.r.local_def_id(item.id);
- // Set access level of nested items.
- // If it's a mod, also make the visitor walk all of its items
- match item.kind {
- // Resolved in rustc_privacy when types are available
- ast::ItemKind::Impl(..) => return,
-
- // Should be unreachable at this stage
- ast::ItemKind::MacCall(..) => panic!(
- "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
- ),
-
- // Foreign modules inherit level from parents.
- ast::ItemKind::ForeignMod(..) => {
- let parent_level =
- self.r.access_levels.map.get(&self.r.local_parent(def_id)).copied();
- self.set_access_level(item.id, parent_level);
- }
-
- // Only exported `macro_rules!` items are public, but they always are
- ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
- if item.attrs.iter().any(|attr| attr.has_name(sym::macro_export)) {
- self.set_access_level(item.id, Some(AccessLevel::Public));
- }
- }
-
- ast::ItemKind::Mod(..) => {
- self.set_bindings_access_level(def_id);
- visit::walk_item(self, item);
- }
-
- ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
- self.set_bindings_access_level(def_id);
- for variant in variants {
- let variant_def_id = self.r.local_def_id(variant.id);
- let variant_level = self.r.access_levels.map.get(&variant_def_id).copied();
- for field in variant.data.fields() {
- self.set_access_level(field.id, variant_level);
- }
- }
- }
-
- ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
- let inherited_level = self.r.access_levels.map.get(&def_id).copied();
- for field in def.fields() {
- if field.vis.kind.is_pub() {
- self.set_access_level(field.id, inherited_level);
- }
- }
- }
-
- ast::ItemKind::Trait(..) => {
- self.set_bindings_access_level(def_id);
- }
-
- ast::ItemKind::ExternCrate(..)
- | ast::ItemKind::Use(..)
- | ast::ItemKind::Static(..)
- | ast::ItemKind::Const(..)
- | ast::ItemKind::GlobalAsm(..)
- | ast::ItemKind::TyAlias(..)
- | ast::ItemKind::TraitAlias(..)
- | ast::ItemKind::MacroDef(..)
- | ast::ItemKind::Fn(..) => return,
- }
- }
-}
diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs
index 81b67b758..a17793ecd 100644
--- a/compiler/rustc_resolve/src/build_reduced_graph.rs
+++ b/compiler/rustc_resolve/src/build_reduced_graph.rs
@@ -326,7 +326,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
PathResult::Module(..) => Err(VisResolutionError::ModuleOnly(path.span)),
PathResult::NonModule(partial_res) => {
- expected_found_error(partial_res.base_res())
+ expected_found_error(partial_res.expect_full_res())
}
PathResult::Failed { span, label, suggestion, .. } => {
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
@@ -1010,7 +1010,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
_,
)
| Res::Local(..)
- | Res::SelfTy { .. }
+ | Res::SelfTyParam { .. }
+ | Res::SelfTyAlias { .. }
| Res::SelfCtor(..)
| Res::Err => bug!("unexpected resolution: {:?}", res),
}
@@ -1424,7 +1425,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
}
(DefKind::AssocFn, ValueNS)
}
- AssocItemKind::TyAlias(..) => (DefKind::AssocTy, TypeNS),
+ AssocItemKind::Type(..) => (DefKind::AssocTy, TypeNS),
AssocItemKind::MacCall(_) => bug!(), // handled above
};
diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs
index 8b58b32b6..01c3801f2 100644
--- a/compiler/rustc_resolve/src/check_unused.rs
+++ b/compiler/rustc_resolve/src/check_unused.rs
@@ -6,7 +6,7 @@
// `use` items.
//
// Unused trait imports can't be checked until the method resolution. We save
-// candidates here, and do the actual check in librustc_typeck/check_unused.rs.
+// candidates here, and do the actual check in rustc_hir_analysis/check_unused.rs.
//
// Checking for unused imports is split into three steps:
//
diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs
index 3f88f44ff..d36e0f61d 100644
--- a/compiler/rustc_resolve/src/def_collector.rs
+++ b/compiler/rustc_resolve/src/def_collector.rs
@@ -1,6 +1,5 @@
use crate::{ImplTraitContext, Resolver};
use rustc_ast::visit::{self, FnKind};
-use rustc_ast::walk_list;
use rustc_ast::*;
use rustc_expand::expand::AstFragment;
use rustc_hir::def_id::LocalDefId;
@@ -148,8 +147,13 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
self.with_parent(return_impl_trait_id, |this| {
this.visit_fn_ret_ty(&sig.decl.output)
});
- let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
- self.with_parent(closure_def, |this| walk_list!(this, visit_block, body));
+ // If this async fn has no body (i.e. it's an async fn signature in a trait)
+ // then the closure_def will never be used, and we should avoid generating a
+ // def-id for it.
+ if let Some(body) = body {
+ let closure_def = self.create_def(closure_id, DefPathData::ClosureExpr, span);
+ self.with_parent(closure_def, |this| this.visit_block(body));
+ }
return;
}
}
@@ -235,7 +239,7 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
let def_data = match &i.kind {
AssocItemKind::Fn(..) | AssocItemKind::Const(..) => DefPathData::ValueNs(i.ident.name),
- AssocItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
+ AssocItemKind::Type(..) => DefPathData::TypeNs(i.ident.name),
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
};
@@ -281,21 +285,6 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
fn visit_ty(&mut self, ty: &'a Ty) {
match ty.kind {
TyKind::MacCall(..) => self.visit_macro_invoc(ty.id),
- TyKind::ImplTrait(node_id, _) => {
- let parent_def = match self.impl_trait_context {
- ImplTraitContext::Universal(item_def) => self.resolver.create_def(
- item_def,
- node_id,
- DefPathData::ImplTrait,
- self.expansion.to_expn_id(),
- ty.span,
- ),
- ImplTraitContext::Existential => {
- self.create_def(node_id, DefPathData::ImplTrait, ty.span)
- }
- };
- self.with_parent(parent_def, |this| visit::walk_ty(this, ty))
- }
_ => visit::walk_ty(self, ty),
}
}
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index ab71fa0bc..5d868ebec 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -24,7 +24,7 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
-use rustc_span::{BytePos, Span};
+use rustc_span::{BytePos, Span, SyntaxContext};
use crate::imports::{Import, ImportKind, ImportResolver};
use crate::late::{PatternSource, Rib};
@@ -47,6 +47,7 @@ pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
/// similarly named label and whether or not it is reachable.
pub(crate) type LabelSuggestion = (Ident, bool);
+#[derive(Debug)]
pub(crate) enum SuggestionTarget {
/// The target has a similar name as the name used by the programmer (probably a typo)
SimilarlyNamed,
@@ -54,6 +55,7 @@ pub(crate) enum SuggestionTarget {
SingleItem,
}
+#[derive(Debug)]
pub(crate) struct TypoSuggestion {
pub candidate: Symbol,
pub res: Res,
@@ -70,6 +72,7 @@ impl TypoSuggestion {
}
/// A free importable items suggested in case of resolution failure.
+#[derive(Debug, Clone)]
pub(crate) struct ImportSuggestion {
pub did: Option<DefId>,
pub descr: &'static str,
@@ -120,7 +123,7 @@ impl<'a> Resolver<'a> {
}
fn report_with_use_injections(&mut self, krate: &Crate) {
- for UseError { mut err, candidates, def_id, instead, suggestion, path } in
+ for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in
self.use_injections.drain(..)
{
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
@@ -128,6 +131,7 @@ impl<'a> Resolver<'a> {
} else {
(None, FoundUse::No)
};
+
if !candidates.is_empty() {
show_candidates(
&self.session,
@@ -137,13 +141,18 @@ impl<'a> Resolver<'a> {
&candidates,
if instead { Instead::Yes } else { Instead::No },
found_use,
- IsPattern::No,
+ DiagnosticMode::Normal,
path,
);
+ err.emit();
} else if let Some((span, msg, sugg, appl)) = suggestion {
err.span_suggestion(span, msg, sugg, appl);
+ err.emit();
+ } else if let [segment] = path.as_slice() && is_call {
+ err.stash(segment.ident.span, rustc_errors::StashKey::CallIntoMethod);
+ } else {
+ err.emit();
}
- err.emit();
}
}
@@ -475,11 +484,12 @@ impl<'a> Resolver<'a> {
module: Module<'a>,
names: &mut Vec<TypoSuggestion>,
filter_fn: &impl Fn(Res) -> bool,
+ ctxt: Option<SyntaxContext>,
) {
for (key, resolution) in self.resolutions(module).borrow().iter() {
if let Some(binding) = resolution.borrow().binding {
let res = binding.res();
- if filter_fn(res) {
+ if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
names.push(TypoSuggestion::typo_from_res(key.ident.name, res));
}
}
@@ -511,24 +521,18 @@ impl<'a> Resolver<'a> {
let sm = self.session.source_map();
let def_id = match outer_res {
- Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => {
- if let Some(impl_span) =
- maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id))
- {
+ Res::SelfTyParam { .. } => {
+ err.span_label(span, "can't use `Self` here");
+ return err;
+ }
+ Res::SelfTyAlias { alias_to: def_id, .. } => {
+ if let Some(impl_span) = self.opt_span(def_id) {
err.span_label(
reduce_impl_span_to_impl_keyword(sm, impl_span),
"`Self` type implicitly declared here, by this `impl`",
);
}
- match (maybe_trait_defid, maybe_impl_defid) {
- (Some(_), None) => {
- err.span_label(span, "can't use `Self` here");
- }
- (_, Some(_)) => {
- err.span_label(span, "use a type here instead");
- }
- (None, None) => bug!("`impl` without trait nor type?"),
- }
+ err.span_label(span, "use a type here instead");
return err;
}
Res::Def(DefKind::TyParam, def_id) => {
@@ -545,8 +549,9 @@ impl<'a> Resolver<'a> {
}
_ => {
bug!(
- "GenericParamsFromOuterFunction should only be used with Res::SelfTy, \
- DefKind::TyParam or DefKind::ConstParam"
+ "GenericParamsFromOuterFunction should only be used with \
+ Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
+ DefKind::ConstParam"
);
}
};
@@ -696,7 +701,7 @@ impl<'a> Resolver<'a> {
&import_suggestions,
Instead::No,
FoundUse::Yes,
- IsPattern::Yes,
+ DiagnosticMode::Pattern,
vec![],
);
}
@@ -1046,6 +1051,19 @@ impl<'a> Resolver<'a> {
err.span_label(trait_item_span, "item in trait");
err
}
+ ResolutionError::TraitImplDuplicate { name, trait_item_span, old_span } => {
+ let mut err = struct_span_err!(
+ self.session,
+ span,
+ E0201,
+ "duplicate definitions with name `{}`:",
+ name,
+ );
+ err.span_label(old_span, "previous definition here");
+ err.span_label(trait_item_span, "item in trait");
+ err.span_label(span, "duplicate definition");
+ err
+ }
ResolutionError::InvalidAsmSym => {
let mut err = self.session.struct_span_err(span, "invalid `sym` operand");
err.span_label(span, "is a local variable");
@@ -1166,10 +1184,10 @@ impl<'a> Resolver<'a> {
Scope::CrateRoot => {
let root_ident = Ident::new(kw::PathRoot, ident.span);
let root_module = this.resolve_crate_root(root_ident);
- this.add_module_candidates(root_module, &mut suggestions, filter_fn);
+ this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
}
Scope::Module(module, _) => {
- this.add_module_candidates(module, &mut suggestions, filter_fn);
+ this.add_module_candidates(module, &mut suggestions, filter_fn, None);
}
Scope::MacroUsePrelude => {
suggestions.extend(this.macro_use_prelude.iter().filter_map(
@@ -1206,7 +1224,7 @@ impl<'a> Resolver<'a> {
Scope::StdLibPrelude => {
if let Some(prelude) = this.prelude {
let mut tmp_suggestions = Vec::new();
- this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn);
+ this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn, None);
suggestions.extend(
tmp_suggestions
.into_iter()
@@ -1479,7 +1497,7 @@ impl<'a> Resolver<'a> {
&import_suggestions,
Instead::No,
FoundUse::Yes,
- IsPattern::No,
+ DiagnosticMode::Normal,
vec![],
);
@@ -2440,12 +2458,34 @@ enum FoundUse {
No,
}
-/// Whether a binding is part of a pattern or an expression. Used for diagnostics.
-enum IsPattern {
+/// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
+enum DiagnosticMode {
+ Normal,
/// The binding is part of a pattern
- Yes,
- /// The binding is part of an expression
- No,
+ Pattern,
+ /// The binding is part of a use statement
+ Import,
+}
+
+pub(crate) fn import_candidates(
+ session: &Session,
+ source_span: &IndexVec<LocalDefId, Span>,
+ err: &mut Diagnostic,
+ // This is `None` if all placement locations are inside expansions
+ use_placement_span: Option<Span>,
+ candidates: &[ImportSuggestion],
+) {
+ show_candidates(
+ session,
+ source_span,
+ err,
+ use_placement_span,
+ candidates,
+ Instead::Yes,
+ FoundUse::Yes,
+ DiagnosticMode::Import,
+ vec![],
+ );
}
/// When an entity with a given name is not available in scope, we search for
@@ -2460,7 +2500,7 @@ fn show_candidates(
candidates: &[ImportSuggestion],
instead: Instead,
found_use: FoundUse,
- is_pattern: IsPattern,
+ mode: DiagnosticMode,
path: Vec<Segment>,
) {
if candidates.is_empty() {
@@ -2495,7 +2535,7 @@ fn show_candidates(
};
let instead = if let Instead::Yes = instead { " instead" } else { "" };
- let mut msg = if let IsPattern::Yes = is_pattern {
+ let mut msg = if let DiagnosticMode::Pattern = mode {
format!(
"if you meant to match on {}{}{}, use the full path in the pattern",
kind, instead, name
@@ -2508,19 +2548,25 @@ fn show_candidates(
err.note(note);
}
- if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) {
- err.span_suggestions(
- span,
- &msg,
- accessible_path_strings.into_iter().map(|a| a.0),
- Applicability::MaybeIncorrect,
- );
- } else if let Some(span) = use_placement_span {
+ if let Some(span) = use_placement_span {
+ let add_use = match mode {
+ DiagnosticMode::Pattern => {
+ err.span_suggestions(
+ span,
+ &msg,
+ accessible_path_strings.into_iter().map(|a| a.0),
+ Applicability::MaybeIncorrect,
+ );
+ return;
+ }
+ DiagnosticMode::Import => "",
+ DiagnosticMode::Normal => "use ",
+ };
for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement
// from the directly following item.
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
- candidate.0 = format!("use {};\n{}", &candidate.0, additional_newline);
+ candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
}
err.span_suggestions(
@@ -2550,11 +2596,14 @@ fn show_candidates(
err.note(&msg);
}
- } else {
+ } else if !matches!(mode, DiagnosticMode::Import) {
assert!(!inaccessible_path_strings.is_empty());
- let prefix =
- if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" };
+ let prefix = if let DiagnosticMode::Pattern = mode {
+ "you might have meant to match on "
+ } else {
+ ""
+ };
if inaccessible_path_strings.len() == 1 {
let (name, descr, def_id, note) = &inaccessible_path_strings[0];
let msg = format!(
@@ -2562,7 +2611,7 @@ fn show_candidates(
prefix,
descr,
name,
- if let IsPattern::Yes = is_pattern { ", which" } else { "" }
+ if let DiagnosticMode::Pattern = mode { ", which" } else { "" }
);
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
diff --git a/compiler/rustc_resolve/src/effective_visibilities.rs b/compiler/rustc_resolve/src/effective_visibilities.rs
new file mode 100644
index 000000000..c40669ac9
--- /dev/null
+++ b/compiler/rustc_resolve/src/effective_visibilities.rs
@@ -0,0 +1,188 @@
+use crate::{ImportKind, NameBindingKind, Resolver};
+use rustc_ast::ast;
+use rustc_ast::visit;
+use rustc_ast::visit::Visitor;
+use rustc_ast::Crate;
+use rustc_ast::EnumDef;
+use rustc_hir::def_id::LocalDefId;
+use rustc_hir::def_id::CRATE_DEF_ID;
+use rustc_middle::middle::privacy::Level;
+use rustc_middle::ty::{DefIdTree, Visibility};
+
+pub struct EffectiveVisibilitiesVisitor<'r, 'a> {
+ r: &'r mut Resolver<'a>,
+ changed: bool,
+}
+
+impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
+ /// Fills the `Resolver::effective_visibilities` table with public & exported items
+ /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
+ /// need access to a TyCtxt for that.
+ pub fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
+ let mut visitor = EffectiveVisibilitiesVisitor { r, changed: false };
+
+ visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, Level::Direct);
+ visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
+
+ while visitor.changed {
+ visitor.reset();
+ visit::walk_crate(&mut visitor, krate);
+ }
+
+ info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
+ }
+
+ fn reset(&mut self) {
+ self.changed = false;
+ }
+
+ /// Update effective visibilities of bindings in the given module,
+ /// including their whole reexport chains.
+ fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
+ assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
+ let module = self.r.get_module(module_id.to_def_id()).unwrap();
+ let resolutions = self.r.resolutions(module);
+
+ for (_, name_resolution) in resolutions.borrow().iter() {
+ if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
+ // Set the given effective visibility level to `Level::Direct` and
+ // sets the rest of the `use` chain to `Level::Reexported` until
+ // we hit the actual exported item.
+
+ // FIXME: tag and is_public() condition should be removed, but assertions occur.
+ let tag = if binding.is_import() { Level::Reexported } else { Level::Direct };
+ if binding.vis.is_public() {
+ let mut prev_parent_id = module_id;
+ let mut level = Level::Direct;
+ while let NameBindingKind::Import { binding: nested_binding, import, .. } =
+ binding.kind
+ {
+ let mut update = |node_id| self.update(
+ self.r.local_def_id(node_id),
+ binding.vis.expect_local(),
+ prev_parent_id,
+ level,
+ );
+ // In theory all the import IDs have individual visibilities and effective
+ // visibilities, but in practice these IDs go straigth to HIR where all
+ // their few uses assume that their (effective) visibility applies to the
+ // whole syntactic `use` item. So we update them all to the maximum value
+ // among the potential individual effective visibilities. Maybe HIR for
+ // imports shouldn't use three IDs at all.
+ update(import.id);
+ if let ImportKind::Single { additional_ids, .. } = import.kind {
+ update(additional_ids.0);
+ update(additional_ids.1);
+ }
+
+ level = Level::Reexported;
+ prev_parent_id = self.r.local_def_id(import.id);
+ binding = nested_binding;
+ }
+ }
+
+ if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
+ self.update(def_id, binding.vis.expect_local(), module_id, tag);
+ }
+ }
+ }
+ }
+
+ fn update(
+ &mut self,
+ def_id: LocalDefId,
+ nominal_vis: Visibility,
+ parent_id: LocalDefId,
+ tag: Level,
+ ) {
+ let module_id = self
+ .r
+ .get_nearest_non_block_module(def_id.to_def_id())
+ .nearest_parent_mod()
+ .expect_local();
+ if nominal_vis == Visibility::Restricted(module_id)
+ || self.r.visibilities[&parent_id] == Visibility::Restricted(module_id)
+ {
+ return;
+ }
+ let mut effective_visibilities = std::mem::take(&mut self.r.effective_visibilities);
+ self.changed |= effective_visibilities.update(
+ def_id,
+ nominal_vis,
+ || Visibility::Restricted(module_id),
+ parent_id,
+ tag,
+ &*self.r,
+ );
+ self.r.effective_visibilities = effective_visibilities;
+ }
+}
+
+impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> {
+ fn visit_item(&mut self, item: &'ast ast::Item) {
+ let def_id = self.r.local_def_id(item.id);
+ // Update effective visibilities of nested items.
+ // If it's a mod, also make the visitor walk all of its items
+ match item.kind {
+ // Resolved in rustc_privacy when types are available
+ ast::ItemKind::Impl(..) => return,
+
+ // Should be unreachable at this stage
+ ast::ItemKind::MacCall(..) => panic!(
+ "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
+ ),
+
+ // Foreign modules inherit level from parents.
+ ast::ItemKind::ForeignMod(..) => {
+ let parent_id = self.r.local_parent(def_id);
+ self.update(def_id, Visibility::Public, parent_id, Level::Direct);
+ }
+
+ // Only exported `macro_rules!` items are public, but they always are
+ ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
+ let parent_id = self.r.local_parent(def_id);
+ let vis = self.r.visibilities[&def_id];
+ self.update(def_id, vis, parent_id, Level::Direct);
+ }
+
+ ast::ItemKind::Mod(..) => {
+ self.set_bindings_effective_visibilities(def_id);
+ visit::walk_item(self, item);
+ }
+
+ ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
+ self.set_bindings_effective_visibilities(def_id);
+ for variant in variants {
+ let variant_def_id = self.r.local_def_id(variant.id);
+ for field in variant.data.fields() {
+ let field_def_id = self.r.local_def_id(field.id);
+ let vis = self.r.visibilities[&field_def_id];
+ self.update(field_def_id, vis, variant_def_id, Level::Direct);
+ }
+ }
+ }
+
+ ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
+ for field in def.fields() {
+ let field_def_id = self.r.local_def_id(field.id);
+ let vis = self.r.visibilities[&field_def_id];
+ self.update(field_def_id, vis, def_id, Level::Direct);
+ }
+ }
+
+ ast::ItemKind::Trait(..) => {
+ self.set_bindings_effective_visibilities(def_id);
+ }
+
+ ast::ItemKind::ExternCrate(..)
+ | ast::ItemKind::Use(..)
+ | ast::ItemKind::Static(..)
+ | ast::ItemKind::Const(..)
+ | ast::ItemKind::GlobalAsm(..)
+ | ast::ItemKind::TyAlias(..)
+ | ast::ItemKind::TraitAlias(..)
+ | ast::ItemKind::MacroDef(..)
+ | ast::ItemKind::Fn(..) => return,
+ }
+ }
+}
diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs
index 2287aa1eb..e0542d547 100644
--- a/compiler/rustc_resolve/src/ident.rs
+++ b/compiler/rustc_resolve/src/ident.rs
@@ -1162,7 +1162,7 @@ impl<'a> Resolver<'a> {
return Res::Err;
}
}
- Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
+ Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
for rib in ribs {
let has_generic_params: HasGenericParams = match rib.kind {
NormalRibKind
@@ -1182,11 +1182,21 @@ impl<'a> Resolver<'a> {
if !(trivial == ConstantHasGenerics::Yes
|| features.generic_const_exprs)
{
- // HACK(min_const_generics): If we encounter `Self` in an anonymous constant
- // we can't easily tell if it's generic at this stage, so we instead remember
- // this and then enforce the self type to be concrete later on.
- if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
- res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
+ // HACK(min_const_generics): If we encounter `Self` in an anonymous
+ // constant we can't easily tell if it's generic at this stage, so
+ // we instead remember this and then enforce the self type to be
+ // concrete later on.
+ if let Res::SelfTyAlias {
+ alias_to: def,
+ forbid_generic: _,
+ is_trait_impl,
+ } = res
+ {
+ res = Res::SelfTyAlias {
+ alias_to: def,
+ forbid_generic: true,
+ is_trait_impl,
+ }
} else {
if let Some(span) = finalize {
self.report_error(
diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs
index c133c272b..f2cc50c19 100644
--- a/compiler/rustc_resolve/src/imports.rs
+++ b/compiler/rustc_resolve/src/imports.rs
@@ -1,9 +1,9 @@
//! A bunch of methods and structures more or less related to resolving imports.
-use crate::diagnostics::Suggestion;
+use crate::diagnostics::{import_candidates, Suggestion};
use crate::Determinacy::{self, *};
-use crate::Namespace::{MacroNS, TypeNS};
-use crate::{module_to_string, names_to_string};
+use crate::Namespace::*;
+use crate::{module_to_string, names_to_string, ImportSuggestion};
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
use crate::{NameBinding, NameBindingKind, PathResult};
@@ -252,7 +252,7 @@ impl<'a> Resolver<'a> {
self.set_binding_parent_module(binding, module);
self.update_resolution(module, key, |this, resolution| {
if let Some(old_binding) = resolution.binding {
- if res == Res::Err {
+ if res == Res::Err && old_binding.res() != Res::Err {
// Do not override real bindings with `Res::Err`s from error recovery.
return Ok(());
}
@@ -381,6 +381,7 @@ struct UnresolvedImportError {
label: Option<String>,
note: Option<String>,
suggestion: Option<Suggestion>,
+ candidate: Option<Vec<ImportSuggestion>>,
}
pub struct ImportResolver<'a, 'b> {
@@ -472,6 +473,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
label: None,
note: None,
suggestion: None,
+ candidate: None,
};
if path.contains("::") {
errors.push((path, err))
@@ -522,6 +524,16 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
}
diag.multipart_suggestion(&msg, suggestions, applicability);
}
+
+ if let Some(candidate) = &err.candidate {
+ import_candidates(
+ self.r.session,
+ &self.r.source_span,
+ &mut diag,
+ Some(err.span),
+ &candidate,
+ )
+ }
}
diag.emit();
@@ -639,6 +651,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
Some(finalize),
ignore_binding,
);
+
let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len;
import.vis.set(orig_vis);
let module = match path_res {
@@ -681,12 +694,14 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
String::from("a similar path exists"),
Applicability::MaybeIncorrect,
)),
+ candidate: None,
},
None => UnresolvedImportError {
span,
label: Some(label),
note: None,
suggestion,
+ candidate: None,
},
};
return Some(err);
@@ -729,6 +744,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
label: Some(String::from("cannot glob-import a module into itself")),
note: None,
suggestion: None,
+ candidate: None,
});
}
}
@@ -894,11 +910,19 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
}
};
+ let parent_suggestion =
+ self.r.lookup_import_candidates(ident, TypeNS, &import.parent_scope, |_| true);
+
Some(UnresolvedImportError {
span: import.span,
label: Some(label),
note,
suggestion,
+ candidate: if !parent_suggestion.is_empty() {
+ Some(parent_suggestion)
+ } else {
+ None
+ },
})
} else {
// `resolve_ident_in_module` reported a privacy error.
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index d8d82e125..00eb768ad 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -19,7 +19,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::DiagnosticId;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
-use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
+use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate};
use rustc_middle::middle::resolve_lifetime::Set1;
use rustc_middle::ty::DefIdTree;
@@ -35,7 +35,6 @@ use std::collections::{hash_map::Entry, BTreeSet};
use std::mem::{replace, take};
mod diagnostics;
-pub(crate) mod lifetimes;
type Res = def::Res<NodeId>;
@@ -226,22 +225,14 @@ enum LifetimeUseSet {
#[derive(Copy, Clone, Debug)]
enum LifetimeRibKind {
- /// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
- Item,
-
+ // -- Ribs introducing named lifetimes
+ //
/// This rib declares generic parameters.
+ /// Only for this kind the `LifetimeRib::bindings` field can be non-empty.
Generics { binder: NodeId, span: Span, kind: LifetimeBinderKind },
- /// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
- /// generics. We are disallowing this until we can decide on how we want to handle non-'static
- /// lifetimes in const generics. See issue #74052 for discussion.
- ConstGeneric,
-
- /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
- /// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
- /// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
- AnonConst,
-
+ // -- Ribs introducing unnamed lifetimes
+ //
/// Create a new anonymous lifetime parameter and reference it.
///
/// If `report_in_path`, report an error when encountering lifetime elision in a path:
@@ -258,16 +249,31 @@ enum LifetimeRibKind {
/// ```
AnonymousCreateParameter { binder: NodeId, report_in_path: bool },
+ /// Replace all anonymous lifetimes by provided lifetime.
+ Elided(LifetimeRes),
+
+ // -- Barrier ribs that stop lifetime lookup, or continue it but produce an error later.
+ //
/// Give a hard error when either `&` or `'_` is written. Used to
/// rule out things like `where T: Foo<'_>`. Does not imply an
/// error on default object bounds (e.g., `Box<dyn Foo>`).
AnonymousReportError,
- /// Replace all anonymous lifetimes by provided lifetime.
- Elided(LifetimeRes),
-
/// Signal we cannot find which should be the anonymous lifetime.
ElisionFailure,
+
+ /// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
+ /// generics. We are disallowing this until we can decide on how we want to handle non-'static
+ /// lifetimes in const generics. See issue #74052 for discussion.
+ ConstGeneric,
+
+ /// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
+ /// This function will emit an error if `generic_const_exprs` is not enabled, the body
+ /// identified by `body_id` is an anonymous constant and `lifetime_ref` is non-static.
+ AnonConst,
+
+ /// This rib acts as a barrier to forbid reference to lifetimes of a parent item.
+ Item,
}
#[derive(Copy, Clone, Debug)]
@@ -415,7 +421,8 @@ impl<'a> PathSource<'a> {
| DefKind::ForeignTy,
_,
) | Res::PrimTy(..)
- | Res::SelfTy { .. }
+ | Res::SelfTyParam { .. }
+ | Res::SelfTyAlias { .. }
),
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
PathSource::Trait(AliasPossibility::Maybe) => {
@@ -449,7 +456,8 @@ impl<'a> PathSource<'a> {
| DefKind::TyAlias
| DefKind::AssocTy,
_,
- ) | Res::SelfTy { .. }
+ ) | Res::SelfTyParam { .. }
+ | Res::SelfTyAlias { .. }
),
PathSource::TraitItem(ns) => match res {
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true,
@@ -517,6 +525,9 @@ struct DiagnosticMetadata<'ast> {
/// Used to detect possible `if let` written without `let` and to provide structured suggestion.
in_if_condition: Option<&'ast Expr>,
+ /// Used to detect possible new binding written without `let` and to provide structured suggestion.
+ in_assignment: Option<&'ast Expr>,
+
/// If we are currently in a trait object definition. Used to point at the bounds when
/// encountering a struct or enum.
current_trait_object: Option<&'ast [ast::GenericBound]>,
@@ -641,8 +652,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
// Check whether we should interpret this as a bare trait object.
if qself.is_none()
&& let Some(partial_res) = self.r.partial_res_map.get(&ty.id)
- && partial_res.unresolved_segments() == 0
- && let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
+ && let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
{
// This path is actually a bare trait object. In case of a bare `Fn`-trait
// object with anonymous lifetimes, we need this rib to correctly place the
@@ -749,35 +759,31 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
match foreign_item.kind {
ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
- self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
- this.with_generic_param_rib(
- &generics.params,
- ItemRibKind(HasGenericParams::Yes(generics.span)),
- LifetimeRibKind::Generics {
- binder: foreign_item.id,
- kind: LifetimeBinderKind::Item,
- span: generics.span,
- },
- |this| visit::walk_foreign_item(this, foreign_item),
- )
- });
+ self.with_generic_param_rib(
+ &generics.params,
+ ItemRibKind(HasGenericParams::Yes(generics.span)),
+ LifetimeRibKind::Generics {
+ binder: foreign_item.id,
+ kind: LifetimeBinderKind::Item,
+ span: generics.span,
+ },
+ |this| visit::walk_foreign_item(this, foreign_item),
+ );
}
ForeignItemKind::Fn(box Fn { ref generics, .. }) => {
- self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
- this.with_generic_param_rib(
- &generics.params,
- ItemRibKind(HasGenericParams::Yes(generics.span)),
- LifetimeRibKind::Generics {
- binder: foreign_item.id,
- kind: LifetimeBinderKind::Function,
- span: generics.span,
- },
- |this| visit::walk_foreign_item(this, foreign_item),
- )
- });
+ self.with_generic_param_rib(
+ &generics.params,
+ ItemRibKind(HasGenericParams::Yes(generics.span)),
+ LifetimeRibKind::Generics {
+ binder: foreign_item.id,
+ kind: LifetimeBinderKind::Function,
+ span: generics.span,
+ },
+ |this| visit::walk_foreign_item(this, foreign_item),
+ );
}
ForeignItemKind::Static(..) => {
- self.with_item_rib(|this| {
+ self.with_static_rib(|this| {
visit::walk_foreign_item(this, foreign_item);
});
}
@@ -806,7 +812,12 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
sig.decl.has_self(),
sig.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
&sig.decl.output,
- )
+ );
+
+ this.record_lifetime_params_for_async(
+ fn_id,
+ sig.header.asyncness.opt_return_id(),
+ );
},
);
return;
@@ -848,41 +859,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
},
);
- // Construct the list of in-scope lifetime parameters for async lowering.
- // We include all lifetime parameters, either named or "Fresh".
- // The order of those parameters does not matter, as long as it is
- // deterministic.
- if let Some((async_node_id, _)) = async_node_id {
- let mut extra_lifetime_params = this
- .r
- .extra_lifetime_params_map
- .get(&fn_id)
- .cloned()
- .unwrap_or_default();
- for rib in this.lifetime_ribs.iter().rev() {
- extra_lifetime_params.extend(
- rib.bindings
- .iter()
- .map(|(&ident, &(node_id, res))| (ident, node_id, res)),
- );
- match rib.kind {
- LifetimeRibKind::Item => break,
- LifetimeRibKind::AnonymousCreateParameter {
- binder, ..
- } => {
- if let Some(earlier_fresh) =
- this.r.extra_lifetime_params_map.get(&binder)
- {
- extra_lifetime_params.extend(earlier_fresh);
- }
- }
- _ => {}
- }
- }
- this.r
- .extra_lifetime_params_map
- .insert(async_node_id, extra_lifetime_params);
- }
+ this.record_lifetime_params_for_async(fn_id, async_node_id);
if let Some(body) = body {
// Ignore errors in function bodies if this is rustdoc
@@ -1421,9 +1398,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
return self.resolve_anonymous_lifetime(lifetime, false);
}
- let mut indices = (0..self.lifetime_ribs.len()).rev();
- for i in &mut indices {
- let rib = &self.lifetime_ribs[i];
+ let mut lifetime_rib_iter = self.lifetime_ribs.iter().rev();
+ while let Some(rib) = lifetime_rib_iter.next() {
let normalized_ident = ident.normalize_to_macros_2_0();
if let Some(&(_, res)) = rib.bindings.get(&normalized_ident) {
self.record_lifetime_res(lifetime.id, res, LifetimeElisionCandidate::Named);
@@ -1453,9 +1429,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
} else {
LifetimeUseSet::Many
}),
- LifetimeRibKind::Generics { .. }
- | LifetimeRibKind::ConstGeneric
- | LifetimeRibKind::AnonConst => None,
+ LifetimeRibKind::Generics { .. } => None,
+ LifetimeRibKind::ConstGeneric | LifetimeRibKind::AnonConst => {
+ span_bug!(ident.span, "unexpected rib kind: {:?}", rib.kind)
+ }
})
.unwrap_or(LifetimeUseSet::Many);
debug!(?use_ctxt, ?use_set);
@@ -1490,13 +1467,16 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
);
return;
}
- _ => {}
+ LifetimeRibKind::AnonymousCreateParameter { .. }
+ | LifetimeRibKind::Elided(_)
+ | LifetimeRibKind::Generics { .. }
+ | LifetimeRibKind::ElisionFailure
+ | LifetimeRibKind::AnonymousReportError => {}
}
}
let mut outer_res = None;
- for i in indices {
- let rib = &self.lifetime_ribs[i];
+ for rib in lifetime_rib_iter {
let normalized_ident = ident.normalize_to_macros_2_0();
if let Some((&outer, _)) = rib.bindings.get_key_value(&normalized_ident) {
outer_res = Some(outer);
@@ -1523,8 +1503,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
count: 1,
};
let elision_candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
- for i in (0..self.lifetime_ribs.len()).rev() {
- let rib = &mut self.lifetime_ribs[i];
+ for rib in self.lifetime_ribs.iter().rev() {
debug!(?rib.kind);
match rib.kind {
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
@@ -1564,9 +1543,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
return;
}
LifetimeRibKind::Item => break,
- LifetimeRibKind::Generics { .. }
- | LifetimeRibKind::ConstGeneric
- | LifetimeRibKind::AnonConst => {}
+ LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
+ LifetimeRibKind::AnonConst => {
+ // There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
+ span_bug!(lifetime.ident.span, "unexpected rib kind: {:?}", rib.kind)
+ }
}
}
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
@@ -1781,9 +1762,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.report_missing_lifetime_specifiers(vec![missing_lifetime], None);
break;
}
- LifetimeRibKind::Generics { .. }
- | LifetimeRibKind::ConstGeneric
- | LifetimeRibKind::AnonConst => {}
+ LifetimeRibKind::Generics { .. } | LifetimeRibKind::ConstGeneric => {}
+ LifetimeRibKind::AnonConst => {
+ // There is always an `Elided(LifetimeRes::Static)` inside an `AnonConst`.
+ span_bug!(elided_lifetime_span, "unexpected rib kind: {:?}", rib.kind)
+ }
}
}
@@ -1896,9 +1879,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
for (index, (pat, ty)) in inputs.enumerate() {
debug!(?pat, ?ty);
- if let Some(pat) = pat {
- self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
- }
+ self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
+ if let Some(pat) = pat {
+ this.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
+ }
+ });
// Record elision candidates only for this parameter.
debug_assert_matches!(self.lifetime_elision_candidates, None);
@@ -1992,11 +1977,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
match ty.kind {
TyKind::ImplicitSelf => true,
TyKind::Path(None, _) => {
- let path_res = self.r.partial_res_map[&ty.id].base_res();
- if let Res::SelfTy { .. } = path_res {
+ let path_res = self.r.partial_res_map[&ty.id].full_res();
+ if let Some(Res::SelfTyParam { .. } | Res::SelfTyAlias { .. }) = path_res {
return true;
}
- Some(path_res) == self.impl_self
+ self.impl_self.is_some() && path_res == self.impl_self
}
_ => false,
}
@@ -2033,7 +2018,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
None
}
})
- .map(|res| res.base_res())
+ .and_then(|res| res.full_res())
.filter(|res| {
// Permit the types that unambiguously always
// result in the same type constructor being used
@@ -2114,7 +2099,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|this| {
let item_def_id = this.r.local_def_id(item.id).to_def_id();
this.with_self_rib(
- Res::SelfTy { trait_: None, alias_to: Some((item_def_id, false)) },
+ Res::SelfTyAlias {
+ alias_to: item_def_id,
+ forbid_generic: false,
+ is_trait_impl: false,
+ },
|this| {
visit::walk_item(this, item);
},
@@ -2228,14 +2217,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
let local_def_id = this.r.local_def_id(item.id).to_def_id();
- this.with_self_rib(
- Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
- |this| {
- this.visit_generics(generics);
- walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
- this.resolve_trait_items(items);
- },
- );
+ this.with_self_rib(Res::SelfTyParam { trait_: local_def_id }, |this| {
+ this.visit_generics(generics);
+ walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
+ this.resolve_trait_items(items);
+ });
},
);
}
@@ -2252,13 +2238,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
let local_def_id = this.r.local_def_id(item.id).to_def_id();
- this.with_self_rib(
- Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
- |this| {
- this.visit_generics(generics);
- walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
- },
- );
+ this.with_self_rib(Res::SelfTyParam { trait_: local_def_id }, |this| {
+ this.visit_generics(generics);
+ walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
+ });
},
);
}
@@ -2270,7 +2253,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
ItemKind::Static(ref ty, _, ref expr) | ItemKind::Const(_, ref ty, ref expr) => {
- self.with_item_rib(|this| {
+ self.with_static_rib(|this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
this.visit_ty(ty);
});
@@ -2465,11 +2448,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.label_ribs.pop();
}
- fn with_item_rib(&mut self, f: impl FnOnce(&mut Self)) {
+ fn with_static_rib(&mut self, f: impl FnOnce(&mut Self)) {
let kind = ItemRibKind(HasGenericParams::No);
- self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
- this.with_rib(ValueNS, kind, |this| this.with_rib(TypeNS, kind, f))
- })
+ self.with_rib(ValueNS, kind, |this| this.with_rib(TypeNS, kind, f))
}
// HACK(min_const_generics,const_evaluatable_unchecked): We
@@ -2562,7 +2543,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
AssocItemKind::Fn(box Fn { generics, .. }) => {
walk_assoc_item(self, generics, LifetimeBinderKind::Function, item);
}
- AssocItemKind::TyAlias(box TyAlias { generics, .. }) => self
+ AssocItemKind::Type(box TyAlias { generics, .. }) => self
.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
walk_assoc_item(this, generics, LifetimeBinderKind::Item, item)
}),
@@ -2595,7 +2576,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
Finalize::new(trait_ref.ref_id, trait_ref.path.span),
);
self.diagnostic_metadata.currently_processing_impl_trait = None;
- if let Some(def_id) = res.base_res().opt_def_id() {
+ if let Some(def_id) = res.expect_full_res().opt_def_id() {
new_id = Some(def_id);
new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
}
@@ -2640,7 +2621,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
// Dummy self type for better errors if `Self` is used in the trait path.
- this.with_self_rib(Res::SelfTy { trait_: None, alias_to: None }, |this| {
+ this.with_self_rib(Res::SelfTyParam { trait_: LOCAL_CRATE.as_def_id() }, |this| {
this.with_lifetime_rib(
LifetimeRibKind::AnonymousCreateParameter {
binder: item_id,
@@ -2664,9 +2645,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
let item_def_id = item_def_id.to_def_id();
- let res = Res::SelfTy {
- trait_: trait_id,
- alias_to: Some((item_def_id, false)),
+ let res = Res::SelfTyAlias {
+ alias_to: item_def_id,
+ forbid_generic: false,
+ is_trait_impl: trait_id.is_some()
};
this.with_self_rib(res, |this| {
if let Some(trait_ref) = opt_trait_reference.as_ref() {
@@ -2682,8 +2664,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
this.with_current_self_type(self_type, |this| {
this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| {
debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)");
+ let mut seen_trait_items = Default::default();
for item in impl_items {
- this.resolve_impl_item(&**item);
+ this.resolve_impl_item(&**item, &mut seen_trait_items);
}
});
});
@@ -2697,7 +2680,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
);
}
- fn resolve_impl_item(&mut self, item: &'ast AssocItem) {
+ fn resolve_impl_item(
+ &mut self,
+ item: &'ast AssocItem,
+ seen_trait_items: &mut FxHashMap<DefId, Span>,
+ ) {
use crate::ResolutionError::*;
match &item.kind {
AssocItemKind::Const(_, ty, default) => {
@@ -2710,6 +2697,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
&item.kind,
ValueNS,
item.span,
+ seen_trait_items,
|i, s, c| ConstNotMemberOfTrait(i, s, c),
);
@@ -2750,6 +2738,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
&item.kind,
ValueNS,
item.span,
+ seen_trait_items,
|i, s, c| MethodNotMemberOfTrait(i, s, c),
);
@@ -2757,8 +2746,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
);
}
- AssocItemKind::TyAlias(box TyAlias { generics, .. }) => {
- debug!("resolve_implementation AssocItemKind::TyAlias");
+ AssocItemKind::Type(box TyAlias { generics, .. }) => {
+ debug!("resolve_implementation AssocItemKind::Type");
// We also need a new scope for the impl item type parameters.
self.with_generic_param_rib(
&generics.params,
@@ -2778,6 +2767,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
&item.kind,
TypeNS,
item.span,
+ seen_trait_items,
|i, s, c| TypeNotMemberOfTrait(i, s, c),
);
@@ -2799,6 +2789,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
kind: &AssocItemKind,
ns: Namespace,
span: Span,
+ seen_trait_items: &mut FxHashMap<DefId, Span>,
err: F,
) where
F: FnOnce(Ident, String, Option<Symbol>) -> ResolutionError<'a>,
@@ -2831,9 +2822,27 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
};
let res = binding.res();
- let Res::Def(def_kind, _) = res else { bug!() };
+ let Res::Def(def_kind, id_in_trait) = res else { bug!() };
+
+ match seen_trait_items.entry(id_in_trait) {
+ Entry::Occupied(entry) => {
+ self.report_error(
+ span,
+ ResolutionError::TraitImplDuplicate {
+ name: ident.name,
+ old_span: *entry.get(),
+ trait_item_span: binding.span,
+ },
+ );
+ return;
+ }
+ Entry::Vacant(entry) => {
+ entry.insert(span);
+ }
+ };
+
match (def_kind, kind) {
- (DefKind::AssocTy, AssocItemKind::TyAlias(..))
+ (DefKind::AssocTy, AssocItemKind::Type(..))
| (DefKind::AssocFn, AssocItemKind::Fn(..))
| (DefKind::AssocConst, AssocItemKind::Const(..)) => {
self.r.record_partial_res(id, PartialRes::new(res));
@@ -2847,7 +2856,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
let (code, kind) = match kind {
AssocItemKind::Const(..) => (rustc_errors::error_code!(E0323), "const"),
AssocItemKind::Fn(..) => (rustc_errors::error_code!(E0324), "method"),
- AssocItemKind::TyAlias(..) => (rustc_errors::error_code!(E0325), "type"),
+ AssocItemKind::Type(..) => (rustc_errors::error_code!(E0325), "type"),
AssocItemKind::MacCall(..) => span_bug!(span, "unexpanded macro"),
};
let trait_path = path_names_to_string(path);
@@ -2865,10 +2874,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
fn resolve_params(&mut self, params: &'ast [Param]) {
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
- for Param { pat, ty, .. } in params {
- self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
+ self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
+ for Param { pat, .. } in params {
+ this.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
+ }
+ });
+ for Param { ty, .. } in params {
self.visit_ty(ty);
- debug!("(resolving function / closure) recorded parameter");
}
}
@@ -2923,7 +2935,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
fn is_base_res_local(&self, nid: NodeId) -> bool {
- matches!(self.r.partial_res_map.get(&nid).map(|res| res.base_res()), Some(Res::Local(..)))
+ matches!(
+ self.r.partial_res_map.get(&nid).map(|res| res.expect_full_res()),
+ Some(Res::Local(..))
+ )
}
/// Checks that all of the arms in an or-pattern have exactly the
@@ -3326,6 +3341,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
instead,
suggestion,
path: path.into(),
+ is_call: source.is_call(),
});
}
@@ -3390,6 +3406,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
instead: false,
suggestion: None,
path: path.into(),
+ is_call: source.is_call(),
});
} else {
err.cancel();
@@ -3408,12 +3425,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
source.defer_to_typeck(),
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
- {
+ Ok(Some(partial_res)) if let Some(res) = partial_res.full_res() => {
+ if source.is_expected(res) || res == Res::Err {
partial_res
} else {
- report_errors(self, Some(partial_res.base_res()))
+ report_errors(self, Some(res))
}
}
@@ -3621,20 +3637,21 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
};
if path.len() > 1
- && result.base_res() != Res::Err
+ && let Some(res) = result.full_res()
+ && res != Res::Err
&& path[0].ident.name != kw::PathRoot
&& path[0].ident.name != kw::DollarCrate
{
let unqualified_result = {
match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) {
- PathResult::NonModule(path_res) => path_res.base_res(),
+ PathResult::NonModule(path_res) => path_res.expect_full_res(),
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
module.res().unwrap()
}
_ => return Ok(Some(result)),
}
};
- if result.base_res() == unqualified_result {
+ if res == unqualified_result {
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
self.r.lint_buffer.buffer_lint(
lint,
@@ -3828,9 +3845,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ExprKind::Field(ref subexpression, _) => {
self.resolve_expr(subexpression, Some(expr));
}
- ExprKind::MethodCall(ref segment, ref arguments, _) => {
- let mut arguments = arguments.iter();
- self.resolve_expr(arguments.next().unwrap(), Some(expr));
+ ExprKind::MethodCall(ref segment, ref receiver, ref arguments, _) => {
+ self.resolve_expr(receiver, Some(expr));
for argument in arguments {
self.resolve_expr(argument, None);
}
@@ -3927,6 +3943,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.resolve_expr(elem, Some(expr));
self.visit_expr(idx);
}
+ ExprKind::Assign(..) => {
+ let old = self.diagnostic_metadata.in_assignment.replace(expr);
+ visit::walk_expr(self, expr);
+ self.diagnostic_metadata.in_assignment = old;
+ }
_ => {
visit::walk_expr(self, expr);
}
@@ -3962,6 +3983,41 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
Some((ident.name, ns)),
)
}
+
+ /// Construct the list of in-scope lifetime parameters for async lowering.
+ /// We include all lifetime parameters, either named or "Fresh".
+ /// The order of those parameters does not matter, as long as it is
+ /// deterministic.
+ fn record_lifetime_params_for_async(
+ &mut self,
+ fn_id: NodeId,
+ async_node_id: Option<(NodeId, Span)>,
+ ) {
+ if let Some((async_node_id, span)) = async_node_id {
+ let mut extra_lifetime_params =
+ self.r.extra_lifetime_params_map.get(&fn_id).cloned().unwrap_or_default();
+ for rib in self.lifetime_ribs.iter().rev() {
+ extra_lifetime_params.extend(
+ rib.bindings.iter().map(|(&ident, &(node_id, res))| (ident, node_id, res)),
+ );
+ match rib.kind {
+ LifetimeRibKind::Item => break,
+ LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
+ if let Some(earlier_fresh) = self.r.extra_lifetime_params_map.get(&binder) {
+ extra_lifetime_params.extend(earlier_fresh);
+ }
+ }
+ LifetimeRibKind::Generics { .. } => {}
+ _ => {
+ // We are in a function definition. We should only find `Generics`
+ // and `AnonymousCreateParameter` inside the innermost `Item`.
+ span_bug!(span, "unexpected rib kind: {:?}", rib.kind)
+ }
+ }
+ }
+ self.r.extra_lifetime_params_map.insert(async_node_id, extra_lifetime_params);
+ }
+ }
}
struct LifetimeCountVisitor<'a, 'b> {
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index 3b095eeeb..850f023b1 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -38,8 +38,8 @@ type Res = def::Res<ast::NodeId>;
/// A field or associated item from self type suggested in case of resolution failure.
enum AssocSuggestion {
Field,
- MethodWithSelf,
- AssocFn,
+ MethodWithSelf { called: bool },
+ AssocFn { called: bool },
AssocType,
AssocConst,
}
@@ -48,8 +48,14 @@ impl AssocSuggestion {
fn action(&self) -> &'static str {
match self {
AssocSuggestion::Field => "use the available field",
- AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path",
- AssocSuggestion::AssocFn => "call the associated function",
+ AssocSuggestion::MethodWithSelf { called: true } => {
+ "call the method with the fully-qualified path"
+ }
+ AssocSuggestion::MethodWithSelf { called: false } => {
+ "refer to the method with the fully-qualified path"
+ }
+ AssocSuggestion::AssocFn { called: true } => "call the associated function",
+ AssocSuggestion::AssocFn { called: false } => "refer to the associated function",
AssocSuggestion::AssocConst => "use the associated `const`",
AssocSuggestion::AssocType => "use the associated type",
}
@@ -130,6 +136,33 @@ pub(super) enum LifetimeElisionCandidate {
Missing(MissingLifetime),
}
+/// Only used for diagnostics.
+#[derive(Debug)]
+struct BaseError {
+ msg: String,
+ fallback_label: String,
+ span: Span,
+ span_label: Option<(Span, &'static str)>,
+ could_be_expr: bool,
+ suggestion: Option<(Span, &'static str, String)>,
+}
+
+#[derive(Debug)]
+enum TypoCandidate {
+ Typo(TypoSuggestion),
+ Shadowed(Res),
+ None,
+}
+
+impl TypoCandidate {
+ fn to_opt_suggestion(self) -> Option<TypoSuggestion> {
+ match self {
+ TypoCandidate::Typo(sugg) => Some(sugg),
+ TypoCandidate::Shadowed(_) | TypoCandidate::None => None,
+ }
+ }
+}
+
impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
fn def_span(&self, def_id: DefId) -> Option<Span> {
match def_id.krate {
@@ -138,35 +171,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}
- /// Handles error reporting for `smart_resolve_path_fragment` function.
- /// Creates base error and amends it with one short label and possibly some longer helps/notes.
- pub(crate) fn smart_resolve_report_errors(
+ fn make_base_error(
&mut self,
path: &[Segment],
span: Span,
source: PathSource<'_>,
res: Option<Res>,
- ) -> (DiagnosticBuilder<'a, ErrorGuaranteed>, Vec<ImportSuggestion>) {
- let ident_span = path.last().map_or(span, |ident| ident.ident.span);
- let ns = source.namespace();
- let is_expected = &|res| source.is_expected(res);
- let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
-
- debug!(?res, ?source);
-
+ ) -> BaseError {
// Make the base error.
- struct BaseError<'a> {
- msg: String,
- fallback_label: String,
- span: Span,
- span_label: Option<(Span, &'a str)>,
- could_be_expr: bool,
- suggestion: Option<(Span, &'a str, String)>,
- }
let mut expected = source.descr_expected();
let path_str = Segment::names_to_string(path);
let item_str = path.last().unwrap().ident;
- let base_error = if let Some(res) = res {
+ if let Some(res) = res {
BaseError {
msg: format!("expected {}, found {} `{}`", expected, res.descr(), path_str),
fallback_label: format!("not a {expected}"),
@@ -277,8 +293,20 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
could_be_expr: false,
suggestion,
}
- };
+ }
+ }
+ /// Handles error reporting for `smart_resolve_path_fragment` function.
+ /// Creates base error and amends it with one short label and possibly some longer helps/notes.
+ pub(crate) fn smart_resolve_report_errors(
+ &mut self,
+ path: &[Segment],
+ span: Span,
+ source: PathSource<'_>,
+ res: Option<Res>,
+ ) -> (DiagnosticBuilder<'a, ErrorGuaranteed>, Vec<ImportSuggestion>) {
+ debug!(?res, ?source);
+ let base_error = self.make_base_error(path, span, source, res);
let code = source.error_code(res.is_some());
let mut err =
self.r.session.struct_span_err_with_code(base_error.span, &base_error.msg, code);
@@ -289,41 +317,79 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_label(span, label);
}
- if let Some(sugg) = base_error.suggestion {
- err.span_suggestion_verbose(sugg.0, sugg.1, sugg.2, Applicability::MaybeIncorrect);
+ if let Some(ref sugg) = base_error.suggestion {
+ err.span_suggestion_verbose(sugg.0, sugg.1, &sugg.2, Applicability::MaybeIncorrect);
}
- if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
- err.multipart_suggestion(
- "you might have meant to write a `struct` literal",
- vec![
- (span.shrink_to_lo(), "{ SomeStruct ".to_string()),
- (span.shrink_to_hi(), "}".to_string()),
- ],
- Applicability::HasPlaceholders,
- );
+ self.suggest_bare_struct_literal(&mut err);
+ self.suggest_pattern_match_with_let(&mut err, source, span);
+
+ self.suggest_self_or_self_ref(&mut err, path, span);
+ self.detect_assoct_type_constraint_meant_as_path(&mut err, &base_error);
+ if self.suggest_self_ty(&mut err, source, path, span)
+ || self.suggest_self_value(&mut err, source, path, span)
+ {
+ return (err, Vec::new());
}
- match (source, self.diagnostic_metadata.in_if_condition) {
- (
- PathSource::Expr(_),
- Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. }),
- ) => {
- // Icky heuristic so we don't suggest:
- // `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
- // `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
- if lhs.is_approximately_pattern() && lhs.span.contains(span) {
- err.span_suggestion_verbose(
- expr_span.shrink_to_lo(),
- "you might have meant to use pattern matching",
- "let ",
- Applicability::MaybeIncorrect,
- );
+
+ let (found, candidates) =
+ self.try_lookup_name_relaxed(&mut err, source, path, span, res, &base_error);
+ if found {
+ return (err, candidates);
+ }
+
+ if !self.type_ascription_suggestion(&mut err, base_error.span) {
+ let mut fallback =
+ self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
+ fallback |= self.suggest_typo(&mut err, source, path, span, &base_error);
+ if fallback {
+ // Fallback label.
+ err.span_label(base_error.span, &base_error.fallback_label);
+ }
+ }
+ self.err_code_special_cases(&mut err, source, path, span);
+
+ (err, candidates)
+ }
+
+ fn detect_assoct_type_constraint_meant_as_path(
+ &self,
+ err: &mut Diagnostic,
+ base_error: &BaseError,
+ ) {
+ let Some(ty) = self.diagnostic_metadata.current_type_path else { return; };
+ let TyKind::Path(_, path) = &ty.kind else { return; };
+ for segment in &path.segments {
+ let Some(params) = &segment.args else { continue; };
+ let ast::GenericArgs::AngleBracketed(ref params) = params.deref() else { continue; };
+ for param in &params.args {
+ let ast::AngleBracketedArg::Constraint(constraint) = param else { continue; };
+ let ast::AssocConstraintKind::Bound { bounds } = &constraint.kind else {
+ continue;
+ };
+ for bound in bounds {
+ let ast::GenericBound::Trait(trait_ref, ast::TraitBoundModifier::None)
+ = bound else
+ {
+ continue;
+ };
+ if base_error.span == trait_ref.span {
+ err.span_suggestion_verbose(
+ constraint.ident.span.between(trait_ref.span),
+ "you might have meant to write a path instead of an associated type bound",
+ "::",
+ Applicability::MachineApplicable,
+ );
+ }
}
}
- _ => {}
}
+ }
+ fn suggest_self_or_self_ref(&mut self, err: &mut Diagnostic, path: &[Segment], span: Span) {
let is_assoc_fn = self.self_type_is_available();
+ let Some(path_last_segment) = path.last() else { return };
+ let item_str = path_last_segment.ident;
// 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(
@@ -358,96 +424,25 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}
}
+ }
- self.detect_assoct_type_constraint_meant_as_path(base_error.span, &mut err);
-
- // Emit special messages for unresolved `Self` and `self`.
- if is_self_type(path, ns) {
- err.code(rustc_errors::error_code!(E0411));
- err.span_label(
- span,
- "`Self` is only available in impls, traits, and type definitions".to_string(),
- );
- if let Some(item_kind) = self.diagnostic_metadata.current_item {
- err.span_label(
- item_kind.ident.span,
- format!(
- "`Self` not allowed in {} {}",
- item_kind.kind.article(),
- item_kind.kind.descr()
- ),
- );
- }
- return (err, Vec::new());
- }
- if is_self_value(path, ns) {
- debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
-
- err.code(rustc_errors::error_code!(E0424));
- err.span_label(span, match source {
- PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed",
- _ => "`self` value is a keyword only available in methods with a `self` parameter",
- });
- if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
- // The current function has a `self' parameter, but we were unable to resolve
- // a reference to `self`. This can only happen if the `self` identifier we
- // are resolving came from a different hygiene context.
- if fn_kind.decl().inputs.get(0).map_or(false, |p| p.is_self()) {
- err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
- } else {
- let doesnt = if is_assoc_fn {
- let (span, sugg) = fn_kind
- .decl()
- .inputs
- .get(0)
- .map(|p| (p.span.shrink_to_lo(), "&self, "))
- .unwrap_or_else(|| {
- // Try to look for the "(" after the function name, if possible.
- // This avoids placing the suggestion into the visibility specifier.
- let span = fn_kind
- .ident()
- .map_or(*span, |ident| span.with_lo(ident.span.hi()));
- (
- self.r
- .session
- .source_map()
- .span_through_char(span, '(')
- .shrink_to_hi(),
- "&self",
- )
- });
- err.span_suggestion_verbose(
- span,
- "add a `self` receiver parameter to make the associated `fn` a method",
- sugg,
- Applicability::MaybeIncorrect,
- );
- "doesn't"
- } else {
- "can't"
- };
- if let Some(ident) = fn_kind.ident() {
- err.span_label(
- ident.span,
- &format!("this function {} have a `self` parameter", doesnt),
- );
- }
- }
- } else if let Some(item_kind) = self.diagnostic_metadata.current_item {
- err.span_label(
- item_kind.ident.span,
- format!(
- "`self` not allowed in {} {}",
- item_kind.kind.article(),
- item_kind.kind.descr()
- ),
- );
- }
- return (err, Vec::new());
- }
-
+ fn try_lookup_name_relaxed(
+ &mut self,
+ err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
+ source: PathSource<'_>,
+ path: &[Segment],
+ span: Span,
+ res: Option<Res>,
+ base_error: &BaseError,
+ ) -> (bool, Vec<ImportSuggestion>) {
// Try to lookup name in more relaxed fashion for better error reporting.
let ident = path.last().unwrap().ident;
+ let is_expected = &|res| source.is_expected(res);
+ let ns = source.namespace();
+ let is_enum_variant = &|res| matches!(res, Res::Def(DefKind::Variant, _));
+ let path_str = Segment::names_to_string(path);
+ let ident_span = path.last().map_or(span, |ident| ident.ident.span);
+
let mut candidates = self
.r
.lookup_import_candidates(ident, ns, &self.parent_scope, is_expected)
@@ -494,7 +489,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
{
// Already reported this issue on the lhs of the type ascription.
err.delay_as_bug();
- return (err, candidates);
+ return (true, candidates);
}
}
@@ -522,10 +517,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
);
}
}
+
// Try Levenshtein algorithm.
- let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
+ let typo_sugg =
+ self.lookup_typo_candidate(path, source.namespace(), is_expected).to_opt_suggestion();
if path.len() == 1 && self.self_type_is_available() {
- if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
+ if let Some(candidate) =
+ self.lookup_assoc_candidate(ident, ns, is_expected, source.is_call())
+ {
let self_is_available = self.self_value_is_available(path[0].ident.span);
match candidate {
AssocSuggestion::Field => {
@@ -540,16 +539,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.span_label(span, "a field by this name exists in `Self`");
}
}
- AssocSuggestion::MethodWithSelf if self_is_available => {
+ AssocSuggestion::MethodWithSelf { called } if self_is_available => {
+ let msg = if called {
+ "you might have meant to call the method"
+ } else {
+ "you might have meant to refer to the method"
+ };
err.span_suggestion(
span,
- "you might have meant to call the method",
+ msg,
format!("self.{path_str}"),
Applicability::MachineApplicable,
);
}
- AssocSuggestion::MethodWithSelf
- | AssocSuggestion::AssocFn
+ AssocSuggestion::MethodWithSelf { .. }
+ | AssocSuggestion::AssocFn { .. }
| AssocSuggestion::AssocConst
| AssocSuggestion::AssocType => {
err.span_suggestion(
@@ -560,8 +564,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
);
}
}
- self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
- return (err, candidates);
+ self.r.add_typo_suggestion(err, typo_sugg, ident_span);
+ return (true, candidates);
}
// If the first argument in call is `self` suggest calling a method.
@@ -579,14 +583,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
format!("self.{path_str}({args_snippet})"),
Applicability::MachineApplicable,
);
- return (err, candidates);
+ return (true, candidates);
}
}
// Try context-dependent help if relaxed lookup didn't work.
if let Some(res) = res {
if self.smart_resolve_context_dependent_help(
- &mut err,
+ err,
span,
source,
res,
@@ -594,106 +598,148 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
&base_error.fallback_label,
) {
// We do this to avoid losing a secondary span when we override the main error span.
- self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
- return (err, candidates);
+ self.r.add_typo_suggestion(err, typo_sugg, ident_span);
+ return (true, candidates);
}
}
+ return (false, candidates);
+ }
+ fn suggest_trait_and_bounds(
+ &mut self,
+ err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
+ source: PathSource<'_>,
+ res: Option<Res>,
+ span: Span,
+ base_error: &BaseError,
+ ) -> bool {
let is_macro =
base_error.span.from_expansion() && base_error.span.desugaring_kind().is_none();
- if !self.type_ascription_suggestion(&mut err, base_error.span) {
- let mut fallback = false;
- if let (
- PathSource::Trait(AliasPossibility::Maybe),
- Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
- false,
- ) = (source, res, is_macro)
- {
- if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
- fallback = true;
- let spans: Vec<Span> = bounds
- .iter()
- .map(|bound| bound.span())
- .filter(|&sp| sp != base_error.span)
- .collect();
+ let mut fallback = false;
- let start_span = bounds[0].span();
- // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
- let end_span = bounds.last().unwrap().span();
- // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
- let last_bound_span = spans.last().cloned().unwrap();
- let mut multi_span: MultiSpan = spans.clone().into();
- for sp in spans {
- let msg = if sp == last_bound_span {
- format!(
- "...because of {these} bound{s}",
- these = pluralize!("this", bounds.len() - 1),
- s = pluralize!(bounds.len() - 1),
- )
- } else {
- String::new()
- };
- multi_span.push_span_label(sp, msg);
- }
- multi_span
- .push_span_label(base_error.span, "expected this type to be a trait...");
- err.span_help(
- multi_span,
- "`+` is used to constrain a \"trait object\" type with lifetimes or \
- auto-traits; structs and enums can't be bound in that way",
- );
- if bounds.iter().all(|bound| match bound {
- ast::GenericBound::Outlives(_) => true,
- ast::GenericBound::Trait(tr, _) => tr.span == base_error.span,
- }) {
- let mut sugg = vec![];
- if base_error.span != start_span {
- sugg.push((start_span.until(base_error.span), String::new()));
- }
- if base_error.span != end_span {
- sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
- }
+ if let (
+ PathSource::Trait(AliasPossibility::Maybe),
+ Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
+ false,
+ ) = (source, res, is_macro)
+ {
+ if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
+ fallback = true;
+ let spans: Vec<Span> = bounds
+ .iter()
+ .map(|bound| bound.span())
+ .filter(|&sp| sp != base_error.span)
+ .collect();
- err.multipart_suggestion(
- "if you meant to use a type and not a trait here, remove the bounds",
- sugg,
- Applicability::MaybeIncorrect,
- );
+ let start_span = bounds[0].span();
+ // `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
+ let end_span = bounds.last().unwrap().span();
+ // `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
+ let last_bound_span = spans.last().cloned().unwrap();
+ let mut multi_span: MultiSpan = spans.clone().into();
+ for sp in spans {
+ let msg = if sp == last_bound_span {
+ format!(
+ "...because of {these} bound{s}",
+ these = pluralize!("this", bounds.len() - 1),
+ s = pluralize!(bounds.len() - 1),
+ )
+ } else {
+ String::new()
+ };
+ multi_span.push_span_label(sp, msg);
+ }
+ multi_span.push_span_label(base_error.span, "expected this type to be a trait...");
+ err.span_help(
+ multi_span,
+ "`+` is used to constrain a \"trait object\" type with lifetimes or \
+ auto-traits; structs and enums can't be bound in that way",
+ );
+ if bounds.iter().all(|bound| match bound {
+ ast::GenericBound::Outlives(_) => true,
+ ast::GenericBound::Trait(tr, _) => tr.span == base_error.span,
+ }) {
+ let mut sugg = vec![];
+ if base_error.span != start_span {
+ sugg.push((start_span.until(base_error.span), String::new()));
+ }
+ if base_error.span != end_span {
+ sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
}
+
+ err.multipart_suggestion(
+ "if you meant to use a type and not a trait here, remove the bounds",
+ sugg,
+ Applicability::MaybeIncorrect,
+ );
}
}
+ }
- fallback |= self.restrict_assoc_type_in_where_clause(span, &mut err);
+ fallback |= self.restrict_assoc_type_in_where_clause(span, err);
+ fallback
+ }
- if !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span) {
- fallback = true;
- match self.diagnostic_metadata.current_let_binding {
- Some((pat_sp, Some(ty_sp), None))
- if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
- {
- err.span_suggestion_short(
- pat_sp.between(ty_sp),
- "use `=` if you meant to assign",
- " = ",
- Applicability::MaybeIncorrect,
- );
- }
- _ => {}
+ fn suggest_typo(
+ &mut self,
+ err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
+ source: PathSource<'_>,
+ path: &[Segment],
+ span: Span,
+ base_error: &BaseError,
+ ) -> bool {
+ let is_expected = &|res| source.is_expected(res);
+ let ident_span = path.last().map_or(span, |ident| ident.ident.span);
+ let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
+ if let TypoCandidate::Shadowed(res) = typo_sugg
+ && let Some(id) = res.opt_def_id()
+ && let Some(sugg_span) = self.r.opt_span(id)
+ {
+ err.span_label(
+ sugg_span,
+ format!("you might have meant to refer to this {}", res.descr()),
+ );
+ return true;
+ }
+ let mut fallback = false;
+ let typo_sugg = typo_sugg.to_opt_suggestion();
+ if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
+ fallback = true;
+ match self.diagnostic_metadata.current_let_binding {
+ Some((pat_sp, Some(ty_sp), None))
+ if ty_sp.contains(base_error.span) && base_error.could_be_expr =>
+ {
+ err.span_suggestion_short(
+ pat_sp.between(ty_sp),
+ "use `=` if you meant to assign",
+ " = ",
+ Applicability::MaybeIncorrect,
+ );
}
-
- // If the trait has a single item (which wasn't matched by Levenshtein), suggest it
- let suggestion = self.get_single_associated_item(&path, &source, is_expected);
- self.r.add_typo_suggestion(&mut err, suggestion, ident_span);
+ _ => {}
}
- if fallback {
- // Fallback label.
- err.span_label(base_error.span, base_error.fallback_label);
+
+ // If the trait has a single item (which wasn't matched by Levenshtein), suggest it
+ let suggestion = self.get_single_associated_item(&path, &source, is_expected);
+ if !self.r.add_typo_suggestion(err, suggestion, ident_span) {
+ fallback = !self.let_binding_suggestion(err, ident_span);
}
}
+ fallback
+ }
+
+ fn err_code_special_cases(
+ &mut self,
+ err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
+ source: PathSource<'_>,
+ path: &[Segment],
+ span: Span,
+ ) {
if let Some(err_code) = &err.code {
if err_code == &rustc_errors::error_code!(E0425) {
for label_rib in &self.label_ribs {
for (label_ident, node_id) in &label_rib.bindings {
+ let ident = path.last().unwrap().ident;
if format!("'{}", ident) == label_ident.to_string() {
err.span_label(label_ident.span, "a label with a similar name exists");
if let PathSource::Expr(Some(Expr {
@@ -724,38 +770,116 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}
}
+ }
- (err, candidates)
+ /// Emit special messages for unresolved `Self` and `self`.
+ fn suggest_self_ty(
+ &mut self,
+ err: &mut Diagnostic,
+ source: PathSource<'_>,
+ path: &[Segment],
+ span: Span,
+ ) -> bool {
+ if !is_self_type(path, source.namespace()) {
+ return false;
+ }
+ err.code(rustc_errors::error_code!(E0411));
+ err.span_label(
+ span,
+ "`Self` is only available in impls, traits, and type definitions".to_string(),
+ );
+ if let Some(item_kind) = self.diagnostic_metadata.current_item {
+ err.span_label(
+ item_kind.ident.span,
+ format!(
+ "`Self` not allowed in {} {}",
+ item_kind.kind.article(),
+ item_kind.kind.descr()
+ ),
+ );
+ }
+ true
}
- fn detect_assoct_type_constraint_meant_as_path(&self, base_span: Span, err: &mut Diagnostic) {
- let Some(ty) = self.diagnostic_metadata.current_type_path else { return; };
- let TyKind::Path(_, path) = &ty.kind else { return; };
- for segment in &path.segments {
- let Some(params) = &segment.args else { continue; };
- let ast::GenericArgs::AngleBracketed(ref params) = params.deref() else { continue; };
- for param in &params.args {
- let ast::AngleBracketedArg::Constraint(constraint) = param else { continue; };
- let ast::AssocConstraintKind::Bound { bounds } = &constraint.kind else {
- continue;
+ fn suggest_self_value(
+ &mut self,
+ err: &mut Diagnostic,
+ source: PathSource<'_>,
+ path: &[Segment],
+ span: Span,
+ ) -> bool {
+ if !is_self_value(path, source.namespace()) {
+ return false;
+ }
+
+ debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
+ err.code(rustc_errors::error_code!(E0424));
+ err.span_label(
+ span,
+ match source {
+ PathSource::Pat => {
+ "`self` value is a keyword and may not be bound to variables or shadowed"
+ }
+ _ => "`self` value is a keyword only available in methods with a `self` parameter",
+ },
+ );
+ let is_assoc_fn = self.self_type_is_available();
+ if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
+ // The current function has a `self' parameter, but we were unable to resolve
+ // a reference to `self`. This can only happen if the `self` identifier we
+ // are resolving came from a different hygiene context.
+ if fn_kind.decl().inputs.get(0).map_or(false, |p| p.is_self()) {
+ err.span_label(*span, "this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters");
+ } else {
+ let doesnt = if is_assoc_fn {
+ let (span, sugg) = fn_kind
+ .decl()
+ .inputs
+ .get(0)
+ .map(|p| (p.span.shrink_to_lo(), "&self, "))
+ .unwrap_or_else(|| {
+ // Try to look for the "(" after the function name, if possible.
+ // This avoids placing the suggestion into the visibility specifier.
+ let span = fn_kind
+ .ident()
+ .map_or(*span, |ident| span.with_lo(ident.span.hi()));
+ (
+ self.r
+ .session
+ .source_map()
+ .span_through_char(span, '(')
+ .shrink_to_hi(),
+ "&self",
+ )
+ });
+ err.span_suggestion_verbose(
+ span,
+ "add a `self` receiver parameter to make the associated `fn` a method",
+ sugg,
+ Applicability::MaybeIncorrect,
+ );
+ "doesn't"
+ } else {
+ "can't"
};
- for bound in bounds {
- let ast::GenericBound::Trait(trait_ref, ast::TraitBoundModifier::None)
- = bound else
- {
- continue;
- };
- if base_span == trait_ref.span {
- err.span_suggestion_verbose(
- constraint.ident.span.between(trait_ref.span),
- "you might have meant to write a path instead of an associated type bound",
- "::",
- Applicability::MachineApplicable,
- );
- }
+ if let Some(ident) = fn_kind.ident() {
+ err.span_label(
+ ident.span,
+ &format!("this function {} have a `self` parameter", doesnt),
+ );
}
}
+ } else if let Some(item_kind) = self.diagnostic_metadata.current_item {
+ err.span_label(
+ item_kind.ident.span,
+ format!(
+ "`self` not allowed in {} {}",
+ item_kind.kind.article(),
+ item_kind.kind.descr()
+ ),
+ );
}
+ true
}
fn suggest_swapping_misplaced_self_ty_and_trait(
@@ -787,6 +911,45 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}
+ fn suggest_bare_struct_literal(&mut self, err: &mut Diagnostic) {
+ if let Some(span) = self.diagnostic_metadata.current_block_could_be_bare_struct_literal {
+ err.multipart_suggestion(
+ "you might have meant to write a `struct` literal",
+ vec![
+ (span.shrink_to_lo(), "{ SomeStruct ".to_string()),
+ (span.shrink_to_hi(), "}".to_string()),
+ ],
+ Applicability::HasPlaceholders,
+ );
+ }
+ }
+
+ fn suggest_pattern_match_with_let(
+ &mut self,
+ err: &mut Diagnostic,
+ source: PathSource<'_>,
+ span: Span,
+ ) {
+ if let PathSource::Expr(_) = source &&
+ let Some(Expr {
+ span: expr_span,
+ kind: ExprKind::Assign(lhs, _, _),
+ ..
+ }) = self.diagnostic_metadata.in_if_condition {
+ // Icky heuristic so we don't suggest:
+ // `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
+ // `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
+ if lhs.is_approximately_pattern() && lhs.span.contains(span) {
+ err.span_suggestion_verbose(
+ expr_span.shrink_to_lo(),
+ "you might have meant to use pattern matching",
+ "let ",
+ Applicability::MaybeIncorrect,
+ );
+ }
+ }
+ }
+
fn get_single_associated_item(
&mut self,
path: &[Segment],
@@ -849,11 +1012,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let Some(partial_res) = self.r.partial_res_map.get(&bounded_ty.id) else {
return false;
};
- if !(matches!(
- partial_res.base_res(),
- hir::def::Res::Def(hir::def::DefKind::AssocTy, _)
- ) && partial_res.unresolved_segments() == 0)
- {
+ if !matches!(
+ partial_res.full_res(),
+ Some(hir::def::Res::Def(hir::def::DefKind::AssocTy, _))
+ ) {
return false;
}
(ty, position, path)
@@ -867,11 +1029,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let Some(partial_res) = self.r.partial_res_map.get(&peeled_ty.id) else {
return false;
};
- if !(matches!(
- partial_res.base_res(),
- hir::def::Res::Def(hir::def::DefKind::TyParam, _)
- ) && partial_res.unresolved_segments() == 0)
- {
+ if !matches!(
+ partial_res.full_res(),
+ Some(hir::def::Res::Def(hir::def::DefKind::TyParam, _))
+ ) {
return false;
}
if let (
@@ -959,41 +1120,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
// where a brace being opened means a block is being started. Look
// ahead for the next text to see if `span` is followed by a `{`.
let sm = self.r.session.source_map();
- let mut sp = span;
- loop {
- sp = sm.next_point(sp);
- match sm.span_to_snippet(sp) {
- Ok(ref snippet) => {
- if snippet.chars().any(|c| !c.is_whitespace()) {
- break;
- }
- }
- _ => break,
- }
- }
+ let sp = sm.span_look_ahead(span, None, Some(50));
let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{");
// In case this could be a struct literal that needs to be surrounded
// by parentheses, find the appropriate span.
- let mut i = 0;
- let mut closing_brace = None;
- loop {
- sp = sm.next_point(sp);
- match sm.span_to_snippet(sp) {
- Ok(ref snippet) => {
- if snippet == "}" {
- closing_brace = Some(span.to(sp));
- break;
- }
- }
- _ => break,
- }
- i += 1;
- // The bigger the span, the more likely we're incorrect --
- // bound it to 100 chars long.
- if i > 100 {
- break;
- }
- }
+ let closing_span = sm.span_look_ahead(span, Some("}"), Some(50));
+ let closing_brace: Option<Span> = sm
+ .span_to_snippet(closing_span)
+ .map_or(None, |s| if s == "}" { Some(span.to(closing_span)) } else { None });
(followed_by_brace, closing_brace)
}
@@ -1017,7 +1151,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let (lhs_span, rhs_span) = match &expr.kind {
ExprKind::Field(base, ident) => (base.span, ident.span),
- ExprKind::MethodCall(_, args, span) => (args[0].span, *span),
+ ExprKind::MethodCall(_, receiver, _, span) => (receiver.span, *span),
_ => return false,
};
@@ -1330,7 +1464,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
Applicability::HasPlaceholders,
);
}
- (Res::SelfTy { .. }, _) if ns == ValueNS => {
+ (Res::SelfTyParam { .. } | Res::SelfTyAlias { .. }, _) if ns == ValueNS => {
err.span_label(span, fallback_label);
err.note("can't use `Self` as a constructor, you must use the implemented struct");
}
@@ -1363,7 +1497,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
.filter(|(_, res)| match (kind, res) {
(AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true,
(AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,
- (AssocItemKind::TyAlias(..), Res::Def(DefKind::AssocTy, _)) => true,
+ (AssocItemKind::Type(..), Res::Def(DefKind::AssocTy, _)) => true,
_ => false,
})
.map(|(key, _)| key.ident.name)
@@ -1377,6 +1511,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
ident: Ident,
ns: Namespace,
filter_fn: FilterFn,
+ called: bool,
) -> Option<AssocSuggestion>
where
FilterFn: Fn(Res) -> bool,
@@ -1399,20 +1534,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
{
// Look for a field with the same name in the current self_type.
if let Some(resolution) = self.r.partial_res_map.get(&node_id) {
- match resolution.base_res() {
- Res::Def(DefKind::Struct | DefKind::Union, did)
- if resolution.unresolved_segments() == 0 =>
- {
- if let Some(field_names) = self.r.field_names.get(&did) {
- if field_names
- .iter()
- .any(|&field_name| ident.name == field_name.node)
- {
- return Some(AssocSuggestion::Field);
- }
+ if let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) =
+ resolution.full_res()
+ {
+ if let Some(field_names) = self.r.field_names.get(&did) {
+ if field_names.iter().any(|&field_name| ident.name == field_name.node) {
+ return Some(AssocSuggestion::Field);
}
}
- _ => {}
}
}
}
@@ -1424,10 +1553,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
return Some(match &assoc_item.kind {
ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst,
ast::AssocItemKind::Fn(box ast::Fn { sig, .. }) if sig.decl.has_self() => {
- AssocSuggestion::MethodWithSelf
+ AssocSuggestion::MethodWithSelf { called }
}
- ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn,
- ast::AssocItemKind::TyAlias(..) => AssocSuggestion::AssocType,
+ ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn { called },
+ ast::AssocItemKind::Type(..) => AssocSuggestion::AssocType,
ast::AssocItemKind::MacCall(_) => continue,
});
}
@@ -1445,10 +1574,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let res = binding.res();
if filter_fn(res) {
if self.r.has_self.contains(&res.def_id()) {
- return Some(AssocSuggestion::MethodWithSelf);
+ return Some(AssocSuggestion::MethodWithSelf { called });
} else {
match res {
- Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn),
+ Res::Def(DefKind::AssocFn, _) => {
+ return Some(AssocSuggestion::AssocFn { called });
+ }
Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst);
}
@@ -1470,22 +1601,38 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
path: &[Segment],
ns: Namespace,
filter_fn: &impl Fn(Res) -> bool,
- ) -> Option<TypoSuggestion> {
+ ) -> TypoCandidate {
let mut names = Vec::new();
if path.len() == 1 {
+ let mut ctxt = path.last().unwrap().ident.span.ctxt();
+
// Search in lexical scope.
// Walk backwards up the ribs in scope and collect candidates.
for rib in self.ribs[ns].iter().rev() {
+ let rib_ctxt = if rib.kind.contains_params() {
+ ctxt.normalize_to_macros_2_0()
+ } else {
+ ctxt.normalize_to_macro_rules()
+ };
+
// Locals and type parameters
for (ident, &res) in &rib.bindings {
- if filter_fn(res) {
+ if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
names.push(TypoSuggestion::typo_from_res(ident.name, res));
}
}
+
+ if let RibKind::MacroDefinition(def) = rib.kind && def == self.r.macro_def(ctxt) {
+ // If an invocation of this macro created `ident`, give up on `ident`
+ // and switch to `ident`'s source from the macro definition.
+ ctxt.remove_mark();
+ continue;
+ }
+
// Items in scope
if let RibKind::ModuleRibKind(module) = rib.kind {
// Items from this module
- self.r.add_module_candidates(module, &mut names, &filter_fn);
+ self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
if let ModuleKind::Block = module.kind {
// We can see through blocks
@@ -1511,7 +1658,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}));
if let Some(prelude) = self.r.prelude {
- self.r.add_module_candidates(prelude, &mut names, &filter_fn);
+ self.r.add_module_candidates(prelude, &mut names, &filter_fn, None);
}
}
break;
@@ -1530,7 +1677,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
self.resolve_path(mod_path, Some(TypeNS), None)
{
- self.r.add_module_candidates(module, &mut names, &filter_fn);
+ self.r.add_module_candidates(module, &mut names, &filter_fn, None);
}
}
@@ -1543,10 +1690,17 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
name,
None,
) {
- Some(found) if found != name => {
- names.into_iter().find(|suggestion| suggestion.candidate == found)
+ Some(found) => {
+ let Some(sugg) = names.into_iter().find(|suggestion| suggestion.candidate == found) else {
+ return TypoCandidate::None;
+ };
+ if found == name {
+ TypoCandidate::Shadowed(sugg.res)
+ } else {
+ TypoCandidate::Typo(sugg)
+ }
}
- _ => None,
+ _ => TypoCandidate::None,
}
}
@@ -1616,26 +1770,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
}
}
if let Ok(base_snippet) = base_snippet {
- let mut sp = after_colon_sp;
- for _ in 0..100 {
- // Try to find an assignment
- sp = sm.next_point(sp);
- let snippet = sm.span_to_snippet(sp.to(sm.next_point(sp)));
- match snippet {
- Ok(ref x) if x.as_str() == "=" => {
- err.span_suggestion(
- base_span,
- "maybe you meant to write an assignment here",
- format!("let {}", base_snippet),
- Applicability::MaybeIncorrect,
- );
- show_label = false;
- break;
- }
- Ok(ref x) if x.as_str() == "\n" => break,
- Err(_) => break,
- Ok(_) => {}
- }
+ // Try to find an assignment
+ let eq_span = sm.span_look_ahead(after_colon_sp, Some("="), Some(50));
+ if let Ok(ref snippet) = sm.span_to_snippet(eq_span) && snippet == "=" {
+ err.span_suggestion(
+ base_span,
+ "maybe you meant to write an assignment here",
+ format!("let {}", base_snippet),
+ Applicability::MaybeIncorrect,
+ );
+ show_label = false;
}
}
}
@@ -1652,6 +1796,31 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
false
}
+ fn let_binding_suggestion(&self, err: &mut Diagnostic, ident_span: Span) -> bool {
+ // try to give a suggestion for this pattern: `name = 1`, which is common in other languages
+ let mut added_suggestion = false;
+ if let Some(Expr { kind: ExprKind::Assign(lhs, _rhs, _), .. }) = self.diagnostic_metadata.in_assignment &&
+ let ast::ExprKind::Path(None, _) = lhs.kind {
+ let sm = self.r.session.source_map();
+ let line_span = sm.span_extend_to_line(ident_span);
+ let ident_name = sm.span_to_snippet(ident_span).unwrap();
+ // HACK(chenyukang): make sure ident_name is at the starting of the line to protect against macros
+ if sm
+ .span_to_snippet(line_span)
+ .map_or(false, |s| s.trim().starts_with(&ident_name))
+ {
+ err.span_suggestion_verbose(
+ ident_span.shrink_to_lo(),
+ "you might have meant to introduce a new binding",
+ "let ".to_string(),
+ Applicability::MaybeIncorrect,
+ );
+ added_suggestion = true;
+ }
+ }
+ added_suggestion
+ }
+
fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion)> {
let mut result = None;
let mut seen_modules = FxHashSet::default();
diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs
deleted file mode 100644
index 4d9704617..000000000
--- a/compiler/rustc_resolve/src/late/lifetimes.rs
+++ /dev/null
@@ -1,1855 +0,0 @@
-//! Resolution of early vs late bound lifetimes.
-//!
-//! Name resolution for lifetimes is performed on the AST and embedded into HIR. From this
-//! information, typechecking needs to transform the lifetime parameters into bound lifetimes.
-//! Lifetimes can be early-bound or late-bound. Construction of typechecking terms needs to visit
-//! the types in HIR to identify late-bound lifetimes and assign their Debruijn indices. This file
-//! is also responsible for assigning their semantics to implicit lifetimes in trait objects.
-
-use rustc_ast::walk_list;
-use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
-use rustc_errors::struct_span_err;
-use rustc_hir as hir;
-use rustc_hir::def::{DefKind, Res};
-use rustc_hir::def_id::LocalDefId;
-use rustc_hir::intravisit::{self, Visitor};
-use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeName, Node};
-use rustc_middle::bug;
-use rustc_middle::hir::map::Map;
-use rustc_middle::hir::nested_filter;
-use rustc_middle::middle::resolve_lifetime::*;
-use rustc_middle::ty::{self, DefIdTree, TyCtxt};
-use rustc_span::def_id::DefId;
-use rustc_span::symbol::{sym, Ident};
-use rustc_span::Span;
-use std::fmt;
-
-trait RegionExt {
- fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region);
-
- fn late(index: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region);
-
- fn id(&self) -> Option<DefId>;
-
- fn shifted(self, amount: u32) -> Region;
-
- fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region;
-}
-
-impl RegionExt for Region {
- fn early(hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) {
- let def_id = hir_map.local_def_id(param.hir_id);
- debug!("Region::early: def_id={:?}", def_id);
- (def_id, Region::EarlyBound(def_id.to_def_id()))
- }
-
- fn late(idx: u32, hir_map: Map<'_>, param: &GenericParam<'_>) -> (LocalDefId, Region) {
- let depth = ty::INNERMOST;
- let def_id = hir_map.local_def_id(param.hir_id);
- debug!(
- "Region::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
- idx, param, depth, def_id,
- );
- (def_id, Region::LateBound(depth, idx, def_id.to_def_id()))
- }
-
- fn id(&self) -> Option<DefId> {
- match *self {
- Region::Static => None,
-
- Region::EarlyBound(id) | Region::LateBound(_, _, id) | Region::Free(_, id) => Some(id),
- }
- }
-
- fn shifted(self, amount: u32) -> Region {
- match self {
- Region::LateBound(debruijn, idx, id) => {
- Region::LateBound(debruijn.shifted_in(amount), idx, id)
- }
- _ => self,
- }
- }
-
- fn shifted_out_to_binder(self, binder: ty::DebruijnIndex) -> Region {
- match self {
- Region::LateBound(debruijn, index, id) => {
- Region::LateBound(debruijn.shifted_out_to_binder(binder), index, id)
- }
- _ => self,
- }
- }
-}
-
-/// Maps the id of each lifetime reference to the lifetime decl
-/// that it corresponds to.
-///
-/// FIXME. This struct gets converted to a `ResolveLifetimes` for
-/// actual use. It has the same data, but indexed by `LocalDefId`. This
-/// is silly.
-#[derive(Debug, Default)]
-struct NamedRegionMap {
- // maps from every use of a named (not anonymous) lifetime to a
- // `Region` describing how that region is bound
- defs: HirIdMap<Region>,
-
- // Maps relevant hir items to the bound vars on them. These include:
- // - function defs
- // - function pointers
- // - closures
- // - trait refs
- // - bound types (like `T` in `for<'a> T<'a>: Foo`)
- late_bound_vars: HirIdMap<Vec<ty::BoundVariableKind>>,
-}
-
-pub(crate) struct LifetimeContext<'a, 'tcx> {
- pub(crate) tcx: TyCtxt<'tcx>,
- map: &'a mut NamedRegionMap,
- scope: ScopeRef<'a>,
-
- /// Indicates that we only care about the definition of a trait. This should
- /// be false if the `Item` we are resolving lifetimes for is not a trait or
- /// we eventually need lifetimes resolve for trait items.
- trait_definition_only: bool,
-}
-
-#[derive(Debug)]
-enum Scope<'a> {
- /// Declares lifetimes, and each can be early-bound or late-bound.
- /// The `DebruijnIndex` of late-bound lifetimes starts at `1` and
- /// it should be shifted by the number of `Binder`s in between the
- /// declaration `Binder` and the location it's referenced from.
- Binder {
- /// We use an IndexMap here because we want these lifetimes in order
- /// for diagnostics.
- lifetimes: FxIndexMap<LocalDefId, Region>,
-
- scope_type: BinderScopeType,
-
- /// The late bound vars for a given item are stored by `HirId` to be
- /// queried later. However, if we enter an elision scope, we have to
- /// later append the elided bound vars to the list and need to know what
- /// to append to.
- hir_id: hir::HirId,
-
- s: ScopeRef<'a>,
-
- /// If this binder comes from a where clause, specify how it was created.
- /// This is used to diagnose inaccessible lifetimes in APIT:
- /// ```ignore (illustrative)
- /// fn foo(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {}
- /// ```
- where_bound_origin: Option<hir::PredicateOrigin>,
- },
-
- /// Lifetimes introduced by a fn are scoped to the call-site for that fn,
- /// if this is a fn body, otherwise the original definitions are used.
- /// Unspecified lifetimes are inferred, unless an elision scope is nested,
- /// e.g., `(&T, fn(&T) -> &T);` becomes `(&'_ T, for<'a> fn(&'a T) -> &'a T)`.
- Body {
- id: hir::BodyId,
- s: ScopeRef<'a>,
- },
-
- /// A scope which either determines unspecified lifetimes or errors
- /// on them (e.g., due to ambiguity).
- Elision {
- s: ScopeRef<'a>,
- },
-
- /// Use a specific lifetime (if `Some`) or leave it unset (to be
- /// inferred in a function body or potentially error outside one),
- /// for the default choice of lifetime in a trait object type.
- ObjectLifetimeDefault {
- lifetime: Option<Region>,
- s: ScopeRef<'a>,
- },
-
- /// When we have nested trait refs, we concatenate late bound vars for inner
- /// trait refs from outer ones. But we also need to include any HRTB
- /// lifetimes encountered when identifying the trait that an associated type
- /// is declared on.
- Supertrait {
- lifetimes: Vec<ty::BoundVariableKind>,
- s: ScopeRef<'a>,
- },
-
- TraitRefBoundary {
- s: ScopeRef<'a>,
- },
-
- Root,
-}
-
-#[derive(Copy, Clone, Debug)]
-enum BinderScopeType {
- /// Any non-concatenating binder scopes.
- Normal,
- /// Within a syntactic trait ref, there may be multiple poly trait refs that
- /// are nested (under the `associated_type_bounds` feature). The binders of
- /// the inner poly trait refs are extended from the outer poly trait refs
- /// and don't increase the late bound depth. If you had
- /// `T: for<'a> Foo<Bar: for<'b> Baz<'a, 'b>>`, then the `for<'b>` scope
- /// would be `Concatenating`. This also used in trait refs in where clauses
- /// where we have two binders `for<> T: for<> Foo` (I've intentionally left
- /// out any lifetimes because they aren't needed to show the two scopes).
- /// The inner `for<>` has a scope of `Concatenating`.
- Concatenating,
-}
-
-// A helper struct for debugging scopes without printing parent scopes
-struct TruncatedScopeDebug<'a>(&'a Scope<'a>);
-
-impl<'a> fmt::Debug for TruncatedScopeDebug<'a> {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.0 {
- Scope::Binder { lifetimes, scope_type, hir_id, where_bound_origin, s: _ } => f
- .debug_struct("Binder")
- .field("lifetimes", lifetimes)
- .field("scope_type", scope_type)
- .field("hir_id", hir_id)
- .field("where_bound_origin", where_bound_origin)
- .field("s", &"..")
- .finish(),
- Scope::Body { id, s: _ } => {
- f.debug_struct("Body").field("id", id).field("s", &"..").finish()
- }
- Scope::Elision { s: _ } => f.debug_struct("Elision").field("s", &"..").finish(),
- Scope::ObjectLifetimeDefault { lifetime, s: _ } => f
- .debug_struct("ObjectLifetimeDefault")
- .field("lifetime", lifetime)
- .field("s", &"..")
- .finish(),
- Scope::Supertrait { lifetimes, s: _ } => f
- .debug_struct("Supertrait")
- .field("lifetimes", lifetimes)
- .field("s", &"..")
- .finish(),
- Scope::TraitRefBoundary { s: _ } => f.debug_struct("TraitRefBoundary").finish(),
- Scope::Root => f.debug_struct("Root").finish(),
- }
- }
-}
-
-type ScopeRef<'a> = &'a Scope<'a>;
-
-const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;
-
-pub fn provide(providers: &mut ty::query::Providers) {
- *providers = ty::query::Providers {
- resolve_lifetimes_trait_definition,
- resolve_lifetimes,
-
- named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
- is_late_bound_map,
- object_lifetime_default,
- late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
-
- ..*providers
- };
-}
-
-/// Like `resolve_lifetimes`, but does not resolve lifetimes for trait items.
-/// Also does not generate any diagnostics.
-///
-/// This is ultimately a subset of the `resolve_lifetimes` work. It effectively
-/// resolves lifetimes only within the trait "header" -- that is, the trait
-/// and supertrait list. In contrast, `resolve_lifetimes` resolves all the
-/// lifetimes within the trait and its items. There is room to refactor this,
-/// for example to resolve lifetimes for each trait item in separate queries,
-/// but it's convenient to do the entire trait at once because the lifetimes
-/// from the trait definition are in scope within the trait items as well.
-///
-/// The reason for this separate call is to resolve what would otherwise
-/// be a cycle. Consider this example:
-///
-/// ```ignore UNSOLVED (maybe @jackh726 knows what lifetime parameter to give Sub)
-/// trait Base<'a> {
-/// type BaseItem;
-/// }
-/// trait Sub<'b>: for<'a> Base<'a> {
-/// type SubItem: Sub<BaseItem = &'b u32>;
-/// }
-/// ```
-///
-/// When we resolve `Sub` and all its items, we also have to resolve `Sub<BaseItem = &'b u32>`.
-/// To figure out the index of `'b`, we have to know about the supertraits
-/// of `Sub` so that we can determine that the `for<'a>` will be in scope.
-/// (This is because we -- currently at least -- flatten all the late-bound
-/// lifetimes into a single binder.) This requires us to resolve the
-/// *trait definition* of `Sub`; basically just enough lifetime information
-/// to look at the supertraits.
-#[instrument(level = "debug", skip(tcx))]
-fn resolve_lifetimes_trait_definition(
- tcx: TyCtxt<'_>,
- local_def_id: LocalDefId,
-) -> ResolveLifetimes {
- convert_named_region_map(do_resolve(tcx, local_def_id, true))
-}
-
-/// Computes the `ResolveLifetimes` map that contains data for an entire `Item`.
-/// You should not read the result of this query directly, but rather use
-/// `named_region_map`, `is_late_bound_map`, etc.
-#[instrument(level = "debug", skip(tcx))]
-fn resolve_lifetimes(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> ResolveLifetimes {
- convert_named_region_map(do_resolve(tcx, local_def_id, false))
-}
-
-fn do_resolve(
- tcx: TyCtxt<'_>,
- local_def_id: LocalDefId,
- trait_definition_only: bool,
-) -> NamedRegionMap {
- let item = tcx.hir().expect_item(local_def_id);
- let mut named_region_map =
- NamedRegionMap { defs: Default::default(), late_bound_vars: Default::default() };
- let mut visitor = LifetimeContext {
- tcx,
- map: &mut named_region_map,
- scope: ROOT_SCOPE,
- trait_definition_only,
- };
- visitor.visit_item(item);
-
- named_region_map
-}
-
-fn convert_named_region_map(named_region_map: NamedRegionMap) -> ResolveLifetimes {
- let mut rl = ResolveLifetimes::default();
-
- for (hir_id, v) in named_region_map.defs {
- let map = rl.defs.entry(hir_id.owner).or_default();
- map.insert(hir_id.local_id, v);
- }
- for (hir_id, v) in named_region_map.late_bound_vars {
- let map = rl.late_bound_vars.entry(hir_id.owner).or_default();
- map.insert(hir_id.local_id, v);
- }
-
- debug!(?rl.defs);
- rl
-}
-
-/// Given `any` owner (structs, traits, trait methods, etc.), does lifetime resolution.
-/// There are two important things this does.
-/// First, we have to resolve lifetimes for
-/// the entire *`Item`* that contains this owner, because that's the largest "scope"
-/// where we can have relevant lifetimes.
-/// Second, if we are asking for lifetimes in a trait *definition*, we use `resolve_lifetimes_trait_definition`
-/// instead of `resolve_lifetimes`, which does not descend into the trait items and does not emit diagnostics.
-/// This allows us to avoid cycles. Importantly, if we ask for lifetimes for lifetimes that have an owner
-/// other than the trait itself (like the trait methods or associated types), then we just use the regular
-/// `resolve_lifetimes`.
-fn resolve_lifetimes_for<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ResolveLifetimes {
- let item_id = item_for(tcx, def_id);
- if item_id == def_id {
- let item = tcx.hir().item(hir::ItemId { def_id: item_id });
- match item.kind {
- hir::ItemKind::Trait(..) => tcx.resolve_lifetimes_trait_definition(item_id),
- _ => tcx.resolve_lifetimes(item_id),
- }
- } else {
- tcx.resolve_lifetimes(item_id)
- }
-}
-
-/// Finds the `Item` that contains the given `LocalDefId`
-fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId {
- match tcx.hir().find_by_def_id(local_def_id) {
- Some(Node::Item(item)) => {
- return item.def_id;
- }
- _ => {}
- }
- let item = {
- let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
- let mut parent_iter = tcx.hir().parent_iter(hir_id);
- loop {
- let node = parent_iter.next().map(|n| n.1);
- match node {
- Some(hir::Node::Item(item)) => break item.def_id,
- Some(hir::Node::Crate(_)) | None => bug!("Called `item_for` on an Item."),
- _ => {}
- }
- }
- };
- item
-}
-
-fn late_region_as_bound_region<'tcx>(tcx: TyCtxt<'tcx>, region: &Region) -> ty::BoundVariableKind {
- match region {
- Region::LateBound(_, _, def_id) => {
- let name = tcx.hir().name(tcx.hir().local_def_id_to_hir_id(def_id.expect_local()));
- ty::BoundVariableKind::Region(ty::BrNamed(*def_id, name))
- }
- _ => bug!("{:?} is not a late region", region),
- }
-}
-
-impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
- /// Returns the binders in scope and the type of `Binder` that should be created for a poly trait ref.
- fn poly_trait_ref_binder_info(&mut self) -> (Vec<ty::BoundVariableKind>, BinderScopeType) {
- let mut scope = self.scope;
- let mut supertrait_lifetimes = vec![];
- loop {
- match scope {
- Scope::Body { .. } | Scope::Root => {
- break (vec![], BinderScopeType::Normal);
- }
-
- Scope::Elision { s, .. } | Scope::ObjectLifetimeDefault { s, .. } => {
- scope = s;
- }
-
- Scope::Supertrait { s, lifetimes } => {
- supertrait_lifetimes = lifetimes.clone();
- scope = s;
- }
-
- Scope::TraitRefBoundary { .. } => {
- // We should only see super trait lifetimes if there is a `Binder` above
- assert!(supertrait_lifetimes.is_empty());
- break (vec![], BinderScopeType::Normal);
- }
-
- Scope::Binder { hir_id, .. } => {
- // Nested poly trait refs have the binders concatenated
- let mut full_binders =
- self.map.late_bound_vars.entry(*hir_id).or_default().clone();
- full_binders.extend(supertrait_lifetimes.into_iter());
- break (full_binders, BinderScopeType::Concatenating);
- }
- }
- }
- }
-}
-impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
- type NestedFilter = nested_filter::All;
-
- fn nested_visit_map(&mut self) -> Self::Map {
- self.tcx.hir()
- }
-
- // We want to nest trait/impl items in their parent, but nothing else.
- fn visit_nested_item(&mut self, _: hir::ItemId) {}
-
- fn visit_trait_item_ref(&mut self, ii: &'tcx hir::TraitItemRef) {
- if !self.trait_definition_only {
- intravisit::walk_trait_item_ref(self, ii)
- }
- }
-
- fn visit_nested_body(&mut self, body: hir::BodyId) {
- let body = self.tcx.hir().body(body);
- self.with(Scope::Body { id: body.id(), s: self.scope }, |this| {
- this.visit_body(body);
- });
- }
-
- fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
- if let hir::ExprKind::Closure(hir::Closure {
- binder, bound_generic_params, fn_decl, ..
- }) = e.kind
- {
- if let &hir::ClosureBinder::For { span: for_sp, .. } = binder {
- fn span_of_infer(ty: &hir::Ty<'_>) -> Option<Span> {
- struct V(Option<Span>);
-
- impl<'v> Visitor<'v> for V {
- fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
- match t.kind {
- _ if self.0.is_some() => (),
- hir::TyKind::Infer => {
- self.0 = Some(t.span);
- }
- _ => intravisit::walk_ty(self, t),
- }
- }
- }
-
- let mut v = V(None);
- v.visit_ty(ty);
- v.0
- }
-
- let infer_in_rt_sp = match fn_decl.output {
- hir::FnRetTy::DefaultReturn(sp) => Some(sp),
- hir::FnRetTy::Return(ty) => span_of_infer(ty),
- };
-
- let infer_spans = fn_decl
- .inputs
- .into_iter()
- .filter_map(span_of_infer)
- .chain(infer_in_rt_sp)
- .collect::<Vec<_>>();
-
- if !infer_spans.is_empty() {
- self.tcx.sess
- .struct_span_err(
- infer_spans,
- "implicit types in closure signatures are forbidden when `for<...>` is present",
- )
- .span_label(for_sp, "`for<...>` is here")
- .emit();
- }
- }
-
- let (lifetimes, binders): (FxIndexMap<LocalDefId, Region>, Vec<_>) =
- bound_generic_params
- .iter()
- .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
- .enumerate()
- .map(|(late_bound_idx, param)| {
- let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
- let r = late_region_as_bound_region(self.tcx, &pair.1);
- (pair, r)
- })
- .unzip();
-
- self.map.late_bound_vars.insert(e.hir_id, binders);
- let scope = Scope::Binder {
- hir_id: e.hir_id,
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
-
- self.with(scope, |this| {
- // a closure has no bounds, so everything
- // contained within is scoped within its binder.
- intravisit::walk_expr(this, e)
- });
- } else {
- intravisit::walk_expr(self, e)
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
- match &item.kind {
- hir::ItemKind::Impl(hir::Impl { of_trait, .. }) => {
- if let Some(of_trait) = of_trait {
- self.map.late_bound_vars.insert(of_trait.hir_ref_id, Vec::default());
- }
- }
- _ => {}
- }
- match item.kind {
- hir::ItemKind::Fn(_, ref generics, _) => {
- self.visit_early_late(item.hir_id(), generics, |this| {
- intravisit::walk_item(this, item);
- });
- }
-
- hir::ItemKind::ExternCrate(_)
- | hir::ItemKind::Use(..)
- | hir::ItemKind::Macro(..)
- | hir::ItemKind::Mod(..)
- | hir::ItemKind::ForeignMod { .. }
- | hir::ItemKind::GlobalAsm(..) => {
- // These sorts of items have no lifetime parameters at all.
- intravisit::walk_item(self, item);
- }
- hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => {
- // No lifetime parameters, but implied 'static.
- self.with(Scope::Elision { s: self.scope }, |this| {
- intravisit::walk_item(this, item)
- });
- }
- hir::ItemKind::OpaqueTy(hir::OpaqueTy { .. }) => {
- // Opaque types are visited when we visit the
- // `TyKind::OpaqueDef`, so that they have the lifetimes from
- // their parent opaque_ty in scope.
- //
- // The core idea here is that since OpaqueTys are generated with the impl Trait as
- // their owner, we can keep going until we find the Item that owns that. We then
- // conservatively add all resolved lifetimes. Otherwise we run into problems in
- // cases like `type Foo<'a> = impl Bar<As = impl Baz + 'a>`.
- for (_hir_id, node) in
- self.tcx.hir().parent_iter(self.tcx.hir().local_def_id_to_hir_id(item.def_id))
- {
- match node {
- hir::Node::Item(parent_item) => {
- let resolved_lifetimes: &ResolveLifetimes =
- self.tcx.resolve_lifetimes(item_for(self.tcx, parent_item.def_id));
- // We need to add *all* deps, since opaque tys may want them from *us*
- for (&owner, defs) in resolved_lifetimes.defs.iter() {
- defs.iter().for_each(|(&local_id, region)| {
- self.map.defs.insert(hir::HirId { owner, local_id }, *region);
- });
- }
- for (&owner, late_bound_vars) in
- resolved_lifetimes.late_bound_vars.iter()
- {
- late_bound_vars.iter().for_each(|(&local_id, late_bound_vars)| {
- self.map.late_bound_vars.insert(
- hir::HirId { owner, local_id },
- late_bound_vars.clone(),
- );
- });
- }
- break;
- }
- hir::Node::Crate(_) => bug!("No Item about an OpaqueTy"),
- _ => {}
- }
- }
- }
- hir::ItemKind::TyAlias(_, ref generics)
- | hir::ItemKind::Enum(_, ref generics)
- | hir::ItemKind::Struct(_, ref generics)
- | hir::ItemKind::Union(_, ref generics)
- | hir::ItemKind::Trait(_, _, ref generics, ..)
- | hir::ItemKind::TraitAlias(ref generics, ..)
- | hir::ItemKind::Impl(hir::Impl { ref generics, .. }) => {
- // These kinds of items have only early-bound lifetime parameters.
- let lifetimes = generics
- .params
- .iter()
- .filter_map(|param| match param.kind {
- GenericParamKind::Lifetime { .. } => {
- Some(Region::early(self.tcx.hir(), param))
- }
- GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
- })
- .collect();
- self.map.late_bound_vars.insert(item.hir_id(), vec![]);
- let scope = Scope::Binder {
- hir_id: item.hir_id(),
- lifetimes,
- scope_type: BinderScopeType::Normal,
- s: ROOT_SCOPE,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- let scope = Scope::TraitRefBoundary { s: this.scope };
- this.with(scope, |this| {
- intravisit::walk_item(this, item);
- });
- });
- }
- }
- }
-
- fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
- match item.kind {
- hir::ForeignItemKind::Fn(_, _, ref generics) => {
- self.visit_early_late(item.hir_id(), generics, |this| {
- intravisit::walk_foreign_item(this, item);
- })
- }
- hir::ForeignItemKind::Static(..) => {
- intravisit::walk_foreign_item(self, item);
- }
- hir::ForeignItemKind::Type => {
- intravisit::walk_foreign_item(self, item);
- }
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
- match ty.kind {
- hir::TyKind::BareFn(ref c) => {
- let (lifetimes, binders): (FxIndexMap<LocalDefId, Region>, Vec<_>) = c
- .generic_params
- .iter()
- .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
- .enumerate()
- .map(|(late_bound_idx, param)| {
- let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
- let r = late_region_as_bound_region(self.tcx, &pair.1);
- (pair, r)
- })
- .unzip();
- self.map.late_bound_vars.insert(ty.hir_id, binders);
- let scope = Scope::Binder {
- hir_id: ty.hir_id,
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- // a bare fn has no bounds, so everything
- // contained within is scoped within its binder.
- intravisit::walk_ty(this, ty);
- });
- }
- hir::TyKind::TraitObject(bounds, ref lifetime, _) => {
- debug!(?bounds, ?lifetime, "TraitObject");
- let scope = Scope::TraitRefBoundary { s: self.scope };
- self.with(scope, |this| {
- for bound in bounds {
- this.visit_poly_trait_ref(bound);
- }
- });
- match lifetime.name {
- LifetimeName::ImplicitObjectLifetimeDefault => {
- // If the user does not write *anything*, we
- // use the object lifetime defaulting
- // rules. So e.g., `Box<dyn Debug>` becomes
- // `Box<dyn Debug + 'static>`.
- self.resolve_object_lifetime_default(lifetime)
- }
- LifetimeName::Infer => {
- // If the user writes `'_`, we use the *ordinary* elision
- // rules. So the `'_` in e.g., `Box<dyn Debug + '_>` will be
- // resolved the same as the `'_` in `&'_ Foo`.
- //
- // cc #48468
- }
- LifetimeName::Param(..) | LifetimeName::Static => {
- // If the user wrote an explicit name, use that.
- self.visit_lifetime(lifetime);
- }
- LifetimeName::Error => {}
- }
- }
- hir::TyKind::Rptr(ref lifetime_ref, ref mt) => {
- self.visit_lifetime(lifetime_ref);
- let scope = Scope::ObjectLifetimeDefault {
- lifetime: self.map.defs.get(&lifetime_ref.hir_id).cloned(),
- s: self.scope,
- };
- self.with(scope, |this| this.visit_ty(&mt.ty));
- }
- hir::TyKind::OpaqueDef(item_id, lifetimes, _in_trait) => {
- // Resolve the lifetimes in the bounds to the lifetime defs in the generics.
- // `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
- // `type MyAnonTy<'b> = impl MyTrait<'b>;`
- // ^ ^ this gets resolved in the scope of
- // the opaque_ty generics
- let opaque_ty = self.tcx.hir().item(item_id);
- let (generics, bounds) = match opaque_ty.kind {
- hir::ItemKind::OpaqueTy(hir::OpaqueTy {
- origin: hir::OpaqueTyOrigin::TyAlias,
- ..
- }) => {
- intravisit::walk_ty(self, ty);
-
- // Elided lifetimes are not allowed in non-return
- // position impl Trait
- let scope = Scope::TraitRefBoundary { s: self.scope };
- self.with(scope, |this| {
- let scope = Scope::Elision { s: this.scope };
- this.with(scope, |this| {
- intravisit::walk_item(this, opaque_ty);
- })
- });
-
- return;
- }
- hir::ItemKind::OpaqueTy(hir::OpaqueTy {
- origin: hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..),
- ref generics,
- bounds,
- ..
- }) => (generics, bounds),
- ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
- };
-
- // Resolve the lifetimes that are applied to the opaque type.
- // These are resolved in the current scope.
- // `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
- // `fn foo<'a>() -> MyAnonTy<'a> { ... }`
- // ^ ^this gets resolved in the current scope
- for lifetime in lifetimes {
- let hir::GenericArg::Lifetime(lifetime) = lifetime else {
- continue
- };
- self.visit_lifetime(lifetime);
-
- // Check for predicates like `impl for<'a> Trait<impl OtherTrait<'a>>`
- // and ban them. Type variables instantiated inside binders aren't
- // well-supported at the moment, so this doesn't work.
- // In the future, this should be fixed and this error should be removed.
- let def = self.map.defs.get(&lifetime.hir_id).cloned();
- let Some(Region::LateBound(_, _, def_id)) = def else {
- continue
- };
- let Some(def_id) = def_id.as_local() else {
- continue
- };
- let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
- // Ensure that the parent of the def is an item, not HRTB
- let parent_id = self.tcx.hir().get_parent_node(hir_id);
- if !parent_id.is_owner() {
- if !self.trait_definition_only {
- struct_span_err!(
- self.tcx.sess,
- lifetime.span,
- E0657,
- "`impl Trait` can only capture lifetimes \
- bound at the fn or impl level"
- )
- .emit();
- }
- self.uninsert_lifetime_on_error(lifetime, def.unwrap());
- }
- if let hir::Node::Item(hir::Item {
- kind: hir::ItemKind::OpaqueTy { .. }, ..
- }) = self.tcx.hir().get(parent_id)
- {
- if !self.trait_definition_only {
- let mut err = self.tcx.sess.struct_span_err(
- lifetime.span,
- "higher kinded lifetime bounds on nested opaque types are not supported yet",
- );
- err.span_note(self.tcx.def_span(def_id), "lifetime declared here");
- err.emit();
- }
- self.uninsert_lifetime_on_error(lifetime, def.unwrap());
- }
- }
-
- // We want to start our early-bound indices at the end of the parent scope,
- // not including any parent `impl Trait`s.
- let mut lifetimes = FxIndexMap::default();
- debug!(?generics.params);
- for param in generics.params {
- match param.kind {
- GenericParamKind::Lifetime { .. } => {
- let (def_id, reg) = Region::early(self.tcx.hir(), &param);
- lifetimes.insert(def_id, reg);
- }
- GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {}
- }
- }
- self.map.late_bound_vars.insert(ty.hir_id, vec![]);
-
- let scope = Scope::Binder {
- hir_id: ty.hir_id,
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- let scope = Scope::TraitRefBoundary { s: this.scope };
- this.with(scope, |this| {
- this.visit_generics(generics);
- for bound in bounds {
- this.visit_param_bound(bound);
- }
- })
- });
- }
- _ => intravisit::walk_ty(self, ty),
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
- use self::hir::TraitItemKind::*;
- match trait_item.kind {
- Fn(_, _) => {
- self.visit_early_late(trait_item.hir_id(), &trait_item.generics, |this| {
- intravisit::walk_trait_item(this, trait_item)
- });
- }
- Type(bounds, ref ty) => {
- let generics = &trait_item.generics;
- let lifetimes = generics
- .params
- .iter()
- .filter_map(|param| match param.kind {
- GenericParamKind::Lifetime { .. } => {
- Some(Region::early(self.tcx.hir(), param))
- }
- GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
- })
- .collect();
- self.map.late_bound_vars.insert(trait_item.hir_id(), vec![]);
- let scope = Scope::Binder {
- hir_id: trait_item.hir_id(),
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- let scope = Scope::TraitRefBoundary { s: this.scope };
- this.with(scope, |this| {
- this.visit_generics(generics);
- for bound in bounds {
- this.visit_param_bound(bound);
- }
- if let Some(ty) = ty {
- this.visit_ty(ty);
- }
- })
- });
- }
- Const(_, _) => {
- // Only methods and types support generics.
- assert!(trait_item.generics.params.is_empty());
- intravisit::walk_trait_item(self, trait_item);
- }
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
- use self::hir::ImplItemKind::*;
- match impl_item.kind {
- Fn(..) => self.visit_early_late(impl_item.hir_id(), &impl_item.generics, |this| {
- intravisit::walk_impl_item(this, impl_item)
- }),
- TyAlias(ref ty) => {
- let generics = &impl_item.generics;
- let lifetimes: FxIndexMap<LocalDefId, Region> = generics
- .params
- .iter()
- .filter_map(|param| match param.kind {
- GenericParamKind::Lifetime { .. } => {
- Some(Region::early(self.tcx.hir(), param))
- }
- GenericParamKind::Const { .. } | GenericParamKind::Type { .. } => None,
- })
- .collect();
- self.map.late_bound_vars.insert(ty.hir_id, vec![]);
- let scope = Scope::Binder {
- hir_id: ty.hir_id,
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- let scope = Scope::TraitRefBoundary { s: this.scope };
- this.with(scope, |this| {
- this.visit_generics(generics);
- this.visit_ty(ty);
- })
- });
- }
- Const(_, _) => {
- // Only methods and types support generics.
- assert!(impl_item.generics.params.is_empty());
- intravisit::walk_impl_item(self, impl_item);
- }
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
- match lifetime_ref.name {
- hir::LifetimeName::Static => self.insert_lifetime(lifetime_ref, Region::Static),
- hir::LifetimeName::Param(param_def_id, _) => {
- self.resolve_lifetime_ref(param_def_id, lifetime_ref)
- }
- // If we've already reported an error, just ignore `lifetime_ref`.
- hir::LifetimeName::Error => {}
- // Those will be resolved by typechecking.
- hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Infer => {}
- }
- }
-
- fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
- for (i, segment) in path.segments.iter().enumerate() {
- let depth = path.segments.len() - i - 1;
- if let Some(ref args) = segment.args {
- self.visit_segment_args(path.res, depth, args);
- }
- }
- }
-
- fn visit_fn(
- &mut self,
- fk: intravisit::FnKind<'tcx>,
- fd: &'tcx hir::FnDecl<'tcx>,
- body_id: hir::BodyId,
- _: Span,
- _: hir::HirId,
- ) {
- let output = match fd.output {
- hir::FnRetTy::DefaultReturn(_) => None,
- hir::FnRetTy::Return(ref ty) => Some(&**ty),
- };
- self.visit_fn_like_elision(&fd.inputs, output, matches!(fk, intravisit::FnKind::Closure));
- intravisit::walk_fn_kind(self, fk);
- self.visit_nested_body(body_id)
- }
-
- fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
- let scope = Scope::TraitRefBoundary { s: self.scope };
- self.with(scope, |this| {
- for param in generics.params {
- match param.kind {
- GenericParamKind::Lifetime { .. } => {}
- GenericParamKind::Type { ref default, .. } => {
- if let Some(ref ty) = default {
- this.visit_ty(&ty);
- }
- }
- GenericParamKind::Const { ref ty, default } => {
- this.visit_ty(&ty);
- if let Some(default) = default {
- this.visit_body(this.tcx.hir().body(default.body));
- }
- }
- }
- }
- for predicate in generics.predicates {
- match predicate {
- &hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
- ref bounded_ty,
- bounds,
- ref bound_generic_params,
- origin,
- ..
- }) => {
- let (lifetimes, binders): (FxIndexMap<LocalDefId, Region>, Vec<_>) =
- bound_generic_params
- .iter()
- .filter(|param| {
- matches!(param.kind, GenericParamKind::Lifetime { .. })
- })
- .enumerate()
- .map(|(late_bound_idx, param)| {
- let pair =
- Region::late(late_bound_idx as u32, this.tcx.hir(), param);
- let r = late_region_as_bound_region(this.tcx, &pair.1);
- (pair, r)
- })
- .unzip();
- this.map.late_bound_vars.insert(bounded_ty.hir_id, binders.clone());
- // Even if there are no lifetimes defined here, we still wrap it in a binder
- // scope. If there happens to be a nested poly trait ref (an error), that
- // will be `Concatenating` anyways, so we don't have to worry about the depth
- // being wrong.
- let scope = Scope::Binder {
- hir_id: bounded_ty.hir_id,
- lifetimes,
- s: this.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: Some(origin),
- };
- this.with(scope, |this| {
- this.visit_ty(&bounded_ty);
- walk_list!(this, visit_param_bound, bounds);
- })
- }
- &hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
- ref lifetime,
- bounds,
- ..
- }) => {
- this.visit_lifetime(lifetime);
- walk_list!(this, visit_param_bound, bounds);
-
- if lifetime.name != hir::LifetimeName::Static {
- for bound in bounds {
- let hir::GenericBound::Outlives(ref lt) = bound else {
- continue;
- };
- if lt.name != hir::LifetimeName::Static {
- continue;
- }
- this.insert_lifetime(lt, Region::Static);
- this.tcx
- .sess
- .struct_span_warn(
- lifetime.span,
- &format!(
- "unnecessary lifetime parameter `{}`",
- lifetime.name.ident(),
- ),
- )
- .help(&format!(
- "you can use the `'static` lifetime directly, in place of `{}`",
- lifetime.name.ident(),
- ))
- .emit();
- }
- }
- }
- &hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
- ref lhs_ty,
- ref rhs_ty,
- ..
- }) => {
- this.visit_ty(lhs_ty);
- this.visit_ty(rhs_ty);
- }
- }
- }
- })
- }
-
- fn visit_param_bound(&mut self, bound: &'tcx hir::GenericBound<'tcx>) {
- match bound {
- hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
- // FIXME(jackh726): This is pretty weird. `LangItemTrait` doesn't go
- // through the regular poly trait ref code, so we don't get another
- // chance to introduce a binder. For now, I'm keeping the existing logic
- // of "if there isn't a Binder scope above us, add one", but I
- // imagine there's a better way to go about this.
- let (binders, scope_type) = self.poly_trait_ref_binder_info();
-
- self.map.late_bound_vars.insert(*hir_id, binders);
- let scope = Scope::Binder {
- hir_id: *hir_id,
- lifetimes: FxIndexMap::default(),
- s: self.scope,
- scope_type,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- intravisit::walk_param_bound(this, bound);
- });
- }
- _ => intravisit::walk_param_bound(self, bound),
- }
- }
-
- fn visit_poly_trait_ref(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) {
- debug!("visit_poly_trait_ref(trait_ref={:?})", trait_ref);
-
- let (mut binders, scope_type) = self.poly_trait_ref_binder_info();
-
- let initial_bound_vars = binders.len() as u32;
- let mut lifetimes: FxIndexMap<LocalDefId, Region> = FxIndexMap::default();
- let binders_iter = trait_ref
- .bound_generic_params
- .iter()
- .filter(|param| matches!(param.kind, GenericParamKind::Lifetime { .. }))
- .enumerate()
- .map(|(late_bound_idx, param)| {
- let pair =
- Region::late(initial_bound_vars + late_bound_idx as u32, self.tcx.hir(), param);
- let r = late_region_as_bound_region(self.tcx, &pair.1);
- lifetimes.insert(pair.0, pair.1);
- r
- });
- binders.extend(binders_iter);
-
- debug!(?binders);
- self.map.late_bound_vars.insert(trait_ref.trait_ref.hir_ref_id, binders);
-
- // Always introduce a scope here, even if this is in a where clause and
- // we introduced the binders around the bounded Ty. In that case, we
- // just reuse the concatenation functionality also present in nested trait
- // refs.
- let scope = Scope::Binder {
- hir_id: trait_ref.trait_ref.hir_ref_id,
- lifetimes,
- s: self.scope,
- scope_type,
- where_bound_origin: None,
- };
- self.with(scope, |this| {
- walk_list!(this, visit_generic_param, trait_ref.bound_generic_params);
- this.visit_trait_ref(&trait_ref.trait_ref);
- });
- }
-}
-
-fn object_lifetime_default<'tcx>(tcx: TyCtxt<'tcx>, param_def_id: DefId) -> ObjectLifetimeDefault {
- debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam);
- let param_def_id = param_def_id.expect_local();
- let parent_def_id = tcx.local_parent(param_def_id);
- let generics = tcx.hir().get_generics(parent_def_id).unwrap();
- let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id);
- let param = generics.params.iter().find(|p| p.hir_id == param_hir_id).unwrap();
-
- // Scan the bounds and where-clauses on parameters to extract bounds
- // of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
- // for each type parameter.
- match param.kind {
- GenericParamKind::Type { .. } => {
- let mut set = Set1::Empty;
-
- // Look for `type: ...` where clauses.
- for bound in generics.bounds_for_param(param_def_id) {
- // Ignore `for<'a> type: ...` as they can change what
- // lifetimes mean (although we could "just" handle it).
- if !bound.bound_generic_params.is_empty() {
- continue;
- }
-
- for bound in bound.bounds {
- if let hir::GenericBound::Outlives(ref lifetime) = *bound {
- set.insert(lifetime.name.normalize_to_macros_2_0());
- }
- }
- }
-
- match set {
- Set1::Empty => ObjectLifetimeDefault::Empty,
- Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
- Set1::One(hir::LifetimeName::Param(param_def_id, _)) => {
- ObjectLifetimeDefault::Param(param_def_id.to_def_id())
- }
- _ => ObjectLifetimeDefault::Ambiguous,
- }
- }
- _ => {
- bug!("object_lifetime_default_raw must only be called on a type parameter")
- }
- }
-}
-
-impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
- fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
- where
- F: for<'b> FnOnce(&mut LifetimeContext<'b, 'tcx>),
- {
- let LifetimeContext { tcx, map, .. } = self;
- let mut this = LifetimeContext {
- tcx: *tcx,
- map,
- scope: &wrap_scope,
- trait_definition_only: self.trait_definition_only,
- };
- let span = debug_span!("scope", scope = ?TruncatedScopeDebug(&this.scope));
- {
- let _enter = span.enter();
- f(&mut this);
- }
- }
-
- /// Visits self by adding a scope and handling recursive walk over the contents with `walk`.
- ///
- /// Handles visiting fns and methods. These are a bit complicated because we must distinguish
- /// early- vs late-bound lifetime parameters. We do this by checking which lifetimes appear
- /// within type bounds; those are early bound lifetimes, and the rest are late bound.
- ///
- /// For example:
- ///
- /// fn foo<'a,'b,'c,T:Trait<'b>>(...)
- ///
- /// Here `'a` and `'c` are late bound but `'b` is early bound. Note that early- and late-bound
- /// lifetimes may be interspersed together.
- ///
- /// If early bound lifetimes are present, we separate them into their own list (and likewise
- /// for late bound). They will be numbered sequentially, starting from the lowest index that is
- /// already in scope (for a fn item, that will be 0, but for a method it might not be). Late
- /// bound lifetimes are resolved by name and associated with a binder ID (`binder_id`), so the
- /// ordering is not important there.
- fn visit_early_late<F>(
- &mut self,
- hir_id: hir::HirId,
- generics: &'tcx hir::Generics<'tcx>,
- walk: F,
- ) where
- F: for<'b, 'c> FnOnce(&'b mut LifetimeContext<'c, 'tcx>),
- {
- let mut named_late_bound_vars = 0;
- let lifetimes: FxIndexMap<LocalDefId, Region> = generics
- .params
- .iter()
- .filter_map(|param| match param.kind {
- GenericParamKind::Lifetime { .. } => {
- if self.tcx.is_late_bound(param.hir_id) {
- let late_bound_idx = named_late_bound_vars;
- named_late_bound_vars += 1;
- Some(Region::late(late_bound_idx, self.tcx.hir(), param))
- } else {
- Some(Region::early(self.tcx.hir(), param))
- }
- }
- GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => None,
- })
- .collect();
-
- let binders: Vec<_> = generics
- .params
- .iter()
- .filter(|param| {
- matches!(param.kind, GenericParamKind::Lifetime { .. })
- && self.tcx.is_late_bound(param.hir_id)
- })
- .enumerate()
- .map(|(late_bound_idx, param)| {
- let pair = Region::late(late_bound_idx as u32, self.tcx.hir(), param);
- late_region_as_bound_region(self.tcx, &pair.1)
- })
- .collect();
- self.map.late_bound_vars.insert(hir_id, binders);
- let scope = Scope::Binder {
- hir_id,
- lifetimes,
- s: self.scope,
- scope_type: BinderScopeType::Normal,
- where_bound_origin: None,
- };
- self.with(scope, walk);
- }
-
- #[instrument(level = "debug", skip(self))]
- fn resolve_lifetime_ref(
- &mut self,
- region_def_id: LocalDefId,
- lifetime_ref: &'tcx hir::Lifetime,
- ) {
- // Walk up the scope chain, tracking the number of fn scopes
- // that we pass through, until we find a lifetime with the
- // given name or we run out of scopes.
- // search.
- let mut late_depth = 0;
- let mut scope = self.scope;
- let mut outermost_body = None;
- let result = loop {
- match *scope {
- Scope::Body { id, s } => {
- outermost_body = Some(id);
- scope = s;
- }
-
- Scope::Root => {
- break None;
- }
-
- Scope::Binder { ref lifetimes, scope_type, s, where_bound_origin, .. } => {
- if let Some(&def) = lifetimes.get(&region_def_id) {
- break Some(def.shifted(late_depth));
- }
- match scope_type {
- BinderScopeType::Normal => late_depth += 1,
- BinderScopeType::Concatenating => {}
- }
- // Fresh lifetimes in APIT used to be allowed in async fns and forbidden in
- // regular fns.
- if let Some(hir::PredicateOrigin::ImplTrait) = where_bound_origin
- && let hir::LifetimeName::Param(_, hir::ParamName::Fresh) = lifetime_ref.name
- && let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner)
- && !self.tcx.features().anonymous_lifetime_in_impl_trait
- {
- rustc_session::parse::feature_err(
- &self.tcx.sess.parse_sess,
- sym::anonymous_lifetime_in_impl_trait,
- lifetime_ref.span,
- "anonymous lifetimes in `impl Trait` are unstable",
- ).emit();
- return;
- }
- scope = s;
- }
-
- Scope::Elision { s, .. }
- | Scope::ObjectLifetimeDefault { s, .. }
- | Scope::Supertrait { s, .. }
- | Scope::TraitRefBoundary { s, .. } => {
- scope = s;
- }
- }
- };
-
- if let Some(mut def) = result {
- if let Region::EarlyBound(..) = def {
- // Do not free early-bound regions, only late-bound ones.
- } else if let Some(body_id) = outermost_body {
- let fn_id = self.tcx.hir().body_owner(body_id);
- match self.tcx.hir().get(fn_id) {
- Node::Item(&hir::Item { kind: hir::ItemKind::Fn(..), .. })
- | Node::TraitItem(&hir::TraitItem {
- kind: hir::TraitItemKind::Fn(..), ..
- })
- | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
- let scope = self.tcx.hir().local_def_id(fn_id);
- def = Region::Free(scope.to_def_id(), def.id().unwrap());
- }
- _ => {}
- }
- }
-
- self.insert_lifetime(lifetime_ref, def);
- return;
- }
-
- // We may fail to resolve higher-ranked lifetimes that are mentioned by APIT.
- // AST-based resolution does not care for impl-trait desugaring, which are the
- // responibility of lowering. This may create a mismatch between the resolution
- // AST found (`region_def_id`) which points to HRTB, and what HIR allows.
- // ```
- // fn foo(x: impl for<'a> Trait<'a, Assoc = impl Copy + 'a>) {}
- // ```
- //
- // In such case, walk back the binders to diagnose it properly.
- let mut scope = self.scope;
- loop {
- match *scope {
- Scope::Binder {
- where_bound_origin: Some(hir::PredicateOrigin::ImplTrait), ..
- } => {
- let mut err = self.tcx.sess.struct_span_err(
- lifetime_ref.span,
- "`impl Trait` can only mention lifetimes bound at the fn or impl level",
- );
- err.span_note(self.tcx.def_span(region_def_id), "lifetime declared here");
- err.emit();
- return;
- }
- Scope::Root => break,
- Scope::Binder { s, .. }
- | Scope::Body { s, .. }
- | Scope::Elision { s, .. }
- | Scope::ObjectLifetimeDefault { s, .. }
- | Scope::Supertrait { s, .. }
- | Scope::TraitRefBoundary { s, .. } => {
- scope = s;
- }
- }
- }
-
- self.tcx.sess.delay_span_bug(
- lifetime_ref.span,
- &format!("Could not resolve {:?} in scope {:#?}", lifetime_ref, self.scope,),
- );
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_segment_args(
- &mut self,
- res: Res,
- depth: usize,
- generic_args: &'tcx hir::GenericArgs<'tcx>,
- ) {
- if generic_args.parenthesized {
- self.visit_fn_like_elision(
- generic_args.inputs(),
- Some(generic_args.bindings[0].ty()),
- false,
- );
- return;
- }
-
- for arg in generic_args.args {
- if let hir::GenericArg::Lifetime(lt) = arg {
- self.visit_lifetime(lt);
- }
- }
-
- // Figure out if this is a type/trait segment,
- // which requires object lifetime defaults.
- let type_def_id = match res {
- Res::Def(DefKind::AssocTy, def_id) if depth == 1 => Some(self.tcx.parent(def_id)),
- Res::Def(DefKind::Variant, def_id) if depth == 0 => Some(self.tcx.parent(def_id)),
- Res::Def(
- DefKind::Struct
- | DefKind::Union
- | DefKind::Enum
- | DefKind::TyAlias
- | DefKind::Trait,
- def_id,
- ) if depth == 0 => Some(def_id),
- _ => None,
- };
-
- debug!(?type_def_id);
-
- // Compute a vector of defaults, one for each type parameter,
- // per the rules given in RFCs 599 and 1156. Example:
- //
- // ```rust
- // struct Foo<'a, T: 'a, U> { }
- // ```
- //
- // If you have `Foo<'x, dyn Bar, dyn Baz>`, we want to default
- // `dyn Bar` to `dyn Bar + 'x` (because of the `T: 'a` bound)
- // and `dyn Baz` to `dyn Baz + 'static` (because there is no
- // such bound).
- //
- // Therefore, we would compute `object_lifetime_defaults` to a
- // vector like `['x, 'static]`. Note that the vector only
- // includes type parameters.
- let object_lifetime_defaults = type_def_id.map_or_else(Vec::new, |def_id| {
- let in_body = {
- let mut scope = self.scope;
- loop {
- match *scope {
- Scope::Root => break false,
-
- Scope::Body { .. } => break true,
-
- Scope::Binder { s, .. }
- | Scope::Elision { s, .. }
- | Scope::ObjectLifetimeDefault { s, .. }
- | Scope::Supertrait { s, .. }
- | Scope::TraitRefBoundary { s, .. } => {
- scope = s;
- }
- }
- }
- };
-
- let map = &self.map;
- let generics = self.tcx.generics_of(def_id);
-
- // `type_def_id` points to an item, so there is nothing to inherit generics from.
- debug_assert_eq!(generics.parent_count, 0);
-
- let set_to_region = |set: ObjectLifetimeDefault| match set {
- ObjectLifetimeDefault::Empty => {
- if in_body {
- None
- } else {
- Some(Region::Static)
- }
- }
- ObjectLifetimeDefault::Static => Some(Region::Static),
- ObjectLifetimeDefault::Param(param_def_id) => {
- // This index can be used with `generic_args` since `parent_count == 0`.
- let index = generics.param_def_id_to_index[&param_def_id] as usize;
- generic_args.args.get(index).and_then(|arg| match arg {
- GenericArg::Lifetime(lt) => map.defs.get(&lt.hir_id).copied(),
- _ => None,
- })
- }
- ObjectLifetimeDefault::Ambiguous => None,
- };
- generics
- .params
- .iter()
- .filter_map(|param| {
- match self.tcx.def_kind(param.def_id) {
- // Generic consts don't impose any constraints.
- //
- // We still store a dummy value here to allow generic parameters
- // in an arbitrary order.
- DefKind::ConstParam => Some(ObjectLifetimeDefault::Empty),
- DefKind::TyParam => Some(self.tcx.object_lifetime_default(param.def_id)),
- // We may also get a `Trait` or `TraitAlias` because of how generics `Self` parameter
- // works. Ignore it because it can't have a meaningful lifetime default.
- DefKind::LifetimeParam | DefKind::Trait | DefKind::TraitAlias => None,
- dk => bug!("unexpected def_kind {:?}", dk),
- }
- })
- .map(set_to_region)
- .collect()
- });
-
- debug!(?object_lifetime_defaults);
-
- let mut i = 0;
- for arg in generic_args.args {
- match arg {
- GenericArg::Lifetime(_) => {}
- GenericArg::Type(ty) => {
- if let Some(&lt) = object_lifetime_defaults.get(i) {
- let scope = Scope::ObjectLifetimeDefault { lifetime: lt, s: self.scope };
- self.with(scope, |this| this.visit_ty(ty));
- } else {
- self.visit_ty(ty);
- }
- i += 1;
- }
- GenericArg::Const(ct) => {
- self.visit_anon_const(&ct.value);
- i += 1;
- }
- GenericArg::Infer(inf) => {
- self.visit_id(inf.hir_id);
- i += 1;
- }
- }
- }
-
- // Hack: when resolving the type `XX` in binding like `dyn
- // Foo<'b, Item = XX>`, the current object-lifetime default
- // would be to examine the trait `Foo` to check whether it has
- // a lifetime bound declared on `Item`. e.g., if `Foo` is
- // declared like so, then the default object lifetime bound in
- // `XX` should be `'b`:
- //
- // ```rust
- // trait Foo<'a> {
- // type Item: 'a;
- // }
- // ```
- //
- // but if we just have `type Item;`, then it would be
- // `'static`. However, we don't get all of this logic correct.
- //
- // Instead, we do something hacky: if there are no lifetime parameters
- // to the trait, then we simply use a default object lifetime
- // bound of `'static`, because there is no other possibility. On the other hand,
- // if there ARE lifetime parameters, then we require the user to give an
- // explicit bound for now.
- //
- // This is intended to leave room for us to implement the
- // correct behavior in the future.
- let has_lifetime_parameter =
- generic_args.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)));
-
- // Resolve lifetimes found in the bindings, so either in the type `XX` in `Item = XX` or
- // in the trait ref `YY<...>` in `Item: YY<...>`.
- for binding in generic_args.bindings {
- let scope = Scope::ObjectLifetimeDefault {
- lifetime: if has_lifetime_parameter { None } else { Some(Region::Static) },
- s: self.scope,
- };
- if let Some(type_def_id) = type_def_id {
- let lifetimes = LifetimeContext::supertrait_hrtb_lifetimes(
- self.tcx,
- type_def_id,
- binding.ident,
- );
- self.with(scope, |this| {
- let scope = Scope::Supertrait {
- lifetimes: lifetimes.unwrap_or_default(),
- s: this.scope,
- };
- this.with(scope, |this| this.visit_assoc_type_binding(binding));
- });
- } else {
- self.with(scope, |this| this.visit_assoc_type_binding(binding));
- }
- }
- }
-
- /// Returns all the late-bound vars that come into scope from supertrait HRTBs, based on the
- /// associated type name and starting trait.
- /// For example, imagine we have
- /// ```ignore (illustrative)
- /// trait Foo<'a, 'b> {
- /// type As;
- /// }
- /// trait Bar<'b>: for<'a> Foo<'a, 'b> {}
- /// trait Bar: for<'b> Bar<'b> {}
- /// ```
- /// In this case, if we wanted to the supertrait HRTB lifetimes for `As` on
- /// the starting trait `Bar`, we would return `Some(['b, 'a])`.
- fn supertrait_hrtb_lifetimes(
- tcx: TyCtxt<'tcx>,
- def_id: DefId,
- assoc_name: Ident,
- ) -> Option<Vec<ty::BoundVariableKind>> {
- let trait_defines_associated_type_named = |trait_def_id: DefId| {
- tcx.associated_items(trait_def_id)
- .find_by_name_and_kind(tcx, assoc_name, ty::AssocKind::Type, trait_def_id)
- .is_some()
- };
-
- use smallvec::{smallvec, SmallVec};
- let mut stack: SmallVec<[(DefId, SmallVec<[ty::BoundVariableKind; 8]>); 8]> =
- smallvec![(def_id, smallvec![])];
- let mut visited: FxHashSet<DefId> = FxHashSet::default();
- loop {
- let Some((def_id, bound_vars)) = stack.pop() else {
- break None;
- };
- // See issue #83753. If someone writes an associated type on a non-trait, just treat it as
- // there being no supertrait HRTBs.
- match tcx.def_kind(def_id) {
- DefKind::Trait | DefKind::TraitAlias | DefKind::Impl => {}
- _ => break None,
- }
-
- if trait_defines_associated_type_named(def_id) {
- break Some(bound_vars.into_iter().collect());
- }
- let predicates =
- tcx.super_predicates_that_define_assoc_type((def_id, Some(assoc_name)));
- let obligations = predicates.predicates.iter().filter_map(|&(pred, _)| {
- let bound_predicate = pred.kind();
- match bound_predicate.skip_binder() {
- ty::PredicateKind::Trait(data) => {
- // The order here needs to match what we would get from `subst_supertrait`
- let pred_bound_vars = bound_predicate.bound_vars();
- let mut all_bound_vars = bound_vars.clone();
- all_bound_vars.extend(pred_bound_vars.iter());
- let super_def_id = data.trait_ref.def_id;
- Some((super_def_id, all_bound_vars))
- }
- _ => None,
- }
- });
-
- let obligations = obligations.filter(|o| visited.insert(o.0));
- stack.extend(obligations);
- }
- }
-
- #[instrument(level = "debug", skip(self))]
- fn visit_fn_like_elision(
- &mut self,
- inputs: &'tcx [hir::Ty<'tcx>],
- output: Option<&'tcx hir::Ty<'tcx>>,
- in_closure: bool,
- ) {
- self.with(Scope::Elision { s: self.scope }, |this| {
- for input in inputs {
- this.visit_ty(input);
- }
- if !in_closure && let Some(output) = output {
- this.visit_ty(output);
- }
- });
- if in_closure && let Some(output) = output {
- self.visit_ty(output);
- }
- }
-
- fn resolve_object_lifetime_default(&mut self, lifetime_ref: &'tcx hir::Lifetime) {
- debug!("resolve_object_lifetime_default(lifetime_ref={:?})", lifetime_ref);
- let mut late_depth = 0;
- let mut scope = self.scope;
- let lifetime = loop {
- match *scope {
- Scope::Binder { s, scope_type, .. } => {
- match scope_type {
- BinderScopeType::Normal => late_depth += 1,
- BinderScopeType::Concatenating => {}
- }
- scope = s;
- }
-
- Scope::Root | Scope::Elision { .. } => break Region::Static,
-
- Scope::Body { .. } | Scope::ObjectLifetimeDefault { lifetime: None, .. } => return,
-
- Scope::ObjectLifetimeDefault { lifetime: Some(l), .. } => break l,
-
- Scope::Supertrait { s, .. } | Scope::TraitRefBoundary { s, .. } => {
- scope = s;
- }
- }
- };
- self.insert_lifetime(lifetime_ref, lifetime.shifted(late_depth));
- }
-
- #[instrument(level = "debug", skip(self))]
- fn insert_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime, def: Region) {
- debug!(
- node = ?self.tcx.hir().node_to_string(lifetime_ref.hir_id),
- span = ?self.tcx.sess.source_map().span_to_diagnostic_string(lifetime_ref.span)
- );
- self.map.defs.insert(lifetime_ref.hir_id, def);
- }
-
- /// Sometimes we resolve a lifetime, but later find that it is an
- /// error (esp. around impl trait). In that case, we remove the
- /// entry into `map.defs` so as not to confuse later code.
- fn uninsert_lifetime_on_error(&mut self, lifetime_ref: &'tcx hir::Lifetime, bad_def: Region) {
- let old_value = self.map.defs.remove(&lifetime_ref.hir_id);
- assert_eq!(old_value, Some(bad_def));
- }
-}
-
-/// Detects late-bound lifetimes and inserts them into
-/// `late_bound`.
-///
-/// A region declared on a fn is **late-bound** if:
-/// - it is constrained by an argument type;
-/// - it does not appear in a where-clause.
-///
-/// "Constrained" basically means that it appears in any type but
-/// not amongst the inputs to a projection. In other words, `<&'a
-/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
-fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxIndexSet<LocalDefId>> {
- let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
- let decl = tcx.hir().fn_decl_by_hir_id(hir_id)?;
- let generics = tcx.hir().get_generics(def_id)?;
-
- let mut late_bound = FxIndexSet::default();
-
- let mut constrained_by_input = ConstrainedCollector::default();
- for arg_ty in decl.inputs {
- constrained_by_input.visit_ty(arg_ty);
- }
-
- let mut appears_in_output = AllCollector::default();
- intravisit::walk_fn_ret_ty(&mut appears_in_output, &decl.output);
-
- debug!(?constrained_by_input.regions);
-
- // Walk the lifetimes that appear in where clauses.
- //
- // Subtle point: because we disallow nested bindings, we can just
- // ignore binders here and scrape up all names we see.
- let mut appears_in_where_clause = AllCollector::default();
- appears_in_where_clause.visit_generics(generics);
- debug!(?appears_in_where_clause.regions);
-
- // Late bound regions are those that:
- // - appear in the inputs
- // - do not appear in the where-clauses
- // - are not implicitly captured by `impl Trait`
- for param in generics.params {
- match param.kind {
- hir::GenericParamKind::Lifetime { .. } => { /* fall through */ }
-
- // Neither types nor consts are late-bound.
- hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue,
- }
-
- let param_def_id = tcx.hir().local_def_id(param.hir_id);
-
- // appears in the where clauses? early-bound.
- if appears_in_where_clause.regions.contains(&param_def_id) {
- continue;
- }
-
- // does not appear in the inputs, but appears in the return type? early-bound.
- if !constrained_by_input.regions.contains(&param_def_id)
- && appears_in_output.regions.contains(&param_def_id)
- {
- continue;
- }
-
- debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.hir_id);
-
- let inserted = late_bound.insert(param_def_id);
- assert!(inserted, "visited lifetime {:?} twice", param.hir_id);
- }
-
- debug!(?late_bound);
- return Some(tcx.arena.alloc(late_bound));
-
- #[derive(Default)]
- struct ConstrainedCollector {
- regions: FxHashSet<LocalDefId>,
- }
-
- impl<'v> Visitor<'v> for ConstrainedCollector {
- fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
- match ty.kind {
- hir::TyKind::Path(
- hir::QPath::Resolved(Some(_), _) | hir::QPath::TypeRelative(..),
- ) => {
- // ignore lifetimes appearing in associated type
- // projections, as they are not *constrained*
- // (defined above)
- }
-
- hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => {
- // consider only the lifetimes on the final
- // segment; I am not sure it's even currently
- // valid to have them elsewhere, but even if it
- // is, those would be potentially inputs to
- // projections
- if let Some(last_segment) = path.segments.last() {
- self.visit_path_segment(last_segment);
- }
- }
-
- _ => {
- intravisit::walk_ty(self, ty);
- }
- }
- }
-
- fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
- if let hir::LifetimeName::Param(def_id, _) = lifetime_ref.name {
- self.regions.insert(def_id);
- }
- }
- }
-
- #[derive(Default)]
- struct AllCollector {
- regions: FxHashSet<LocalDefId>,
- }
-
- impl<'v> Visitor<'v> for AllCollector {
- fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
- if let hir::LifetimeName::Param(def_id, _) = lifetime_ref.name {
- self.regions.insert(def_id);
- }
- }
- }
-}
diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs
index 26b9284fe..8aebb7da1 100644
--- a/compiler/rustc_resolve/src/lib.rs
+++ b/compiler/rustc_resolve/src/lib.rs
@@ -4,7 +4,7 @@
//! Paths in macros, imports, expressions, types, patterns are resolved here.
//! Label and lifetime names are resolved here as well.
//!
-//! Type-relative name resolution (methods, fields, associated items) happens in `rustc_typeck`.
+//! Type-relative name resolution (methods, fields, associated items) happens in `rustc_hir_analysis`.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(assert_matches)]
@@ -13,7 +13,6 @@
#![feature(if_let_guard)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
-#![cfg_attr(bootstrap, feature(let_else))]
#![feature(never_type)]
#![recursion_limit = "256"]
#![allow(rustdoc::private_intra_doc_links)]
@@ -42,12 +41,12 @@ use rustc_hir::TraitCandidate;
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::middle::privacy::EffectiveVisibilities;
use rustc_middle::span_bug;
-use rustc_middle::ty::query::Providers;
-use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, ResolverOutputs};
+use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools};
+use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
use rustc_query_system::ich::StableHashingContext;
-use rustc_session::cstore::{CrateStore, CrateStoreDyn, MetadataLoaderDyn};
+use rustc_session::cstore::{CrateStore, MetadataLoaderDyn};
use rustc_session::lint::LintBuffer;
use rustc_session::Session;
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
@@ -65,15 +64,15 @@ use imports::{Import, ImportKind, ImportResolver, NameResolution};
use late::{HasGenericParams, PathSource, PatternSource};
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
-use crate::access_levels::AccessLevelsVisitor;
+use crate::effective_visibilities::EffectiveVisibilitiesVisitor;
type Res = def::Res<NodeId>;
-mod access_levels;
mod build_reduced_graph;
mod check_unused;
mod def_collector;
mod diagnostics;
+mod effective_visibilities;
mod ident;
mod imports;
mod late;
@@ -238,6 +237,8 @@ enum ResolutionError<'a> {
trait_item_span: Span,
code: rustc_errors::DiagnosticId,
},
+ /// Error E0201: multiple impl items for the same trait item.
+ TraitImplDuplicate { name: Symbol, trait_item_span: Span, old_span: Span },
/// Inline asm `sym` operand must refer to a `fn` or `static`.
InvalidAsmSym,
}
@@ -676,6 +677,8 @@ struct UseError<'a> {
/// Path `Segment`s at the place of use that failed. Used for accurate suggestion after telling
/// the user to import the item directly.
path: Vec<Segment>,
+ /// Whether the expected source is a call
+ is_call: bool,
}
#[derive(Clone, Copy, PartialEq, Debug)]
@@ -1028,7 +1031,7 @@ pub struct Resolver<'a> {
proc_macros: Vec<NodeId>,
confused_type_with_std_module: FxHashMap<Span, Span>,
- access_levels: AccessLevels,
+ effective_visibilities: EffectiveVisibilities,
}
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1332,7 +1335,7 @@ impl<'a> Resolver<'a> {
trait_impls: Default::default(),
proc_macros: Default::default(),
confused_type_with_std_module: Default::default(),
- access_levels: Default::default(),
+ effective_visibilities: Default::default(),
};
let root_parent_scope = ParentScope::module(graph_root, &resolver);
@@ -1375,9 +1378,7 @@ impl<'a> Resolver<'a> {
Default::default()
}
- pub fn into_outputs(
- self,
- ) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
+ pub fn into_outputs(self) -> ResolverOutputs {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions;
let cstore = Box::new(self.crate_loader.into_cstore());
@@ -1392,13 +1393,14 @@ impl<'a> Resolver<'a> {
let glob_map = self.glob_map;
let main_def = self.main_def;
let confused_type_with_std_module = self.confused_type_with_std_module;
- let access_levels = self.access_levels;
- let resolutions = ResolverOutputs {
+ let effective_visibilities = self.effective_visibilities;
+ let global_ctxt = ResolverGlobalCtxt {
+ cstore,
source_span,
expn_that_defined,
visibilities,
has_pub_restricted,
- access_levels,
+ effective_visibilities,
extern_crate_map,
reexport_map,
glob_map,
@@ -1415,7 +1417,7 @@ impl<'a> Resolver<'a> {
confused_type_with_std_module,
registered_tools: self.registered_tools,
};
- let resolutions_lowering = ty::ResolverAstLowering {
+ let ast_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args,
partial_res_map: self.partial_res_map,
import_res_map: self.import_res_map,
@@ -1428,16 +1430,15 @@ impl<'a> Resolver<'a> {
trait_map: self.trait_map,
builtin_macro_kinds: self.builtin_macro_kinds,
};
- (definitions, cstore, resolutions, resolutions_lowering)
+ ResolverOutputs { definitions, global_ctxt, ast_lowering }
}
- pub fn clone_outputs(
- &self,
- ) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
+ pub fn clone_outputs(&self) -> ResolverOutputs {
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
let definitions = self.definitions.clone();
let cstore = Box::new(self.cstore().clone());
- let resolutions = ResolverOutputs {
+ let global_ctxt = ResolverGlobalCtxt {
+ cstore,
source_span: self.source_span.clone(),
expn_that_defined: self.expn_that_defined.clone(),
visibilities: self.visibilities.clone(),
@@ -1457,9 +1458,9 @@ impl<'a> Resolver<'a> {
proc_macros,
confused_type_with_std_module: self.confused_type_with_std_module.clone(),
registered_tools: self.registered_tools.clone(),
- access_levels: self.access_levels.clone(),
+ effective_visibilities: self.effective_visibilities.clone(),
};
- let resolutions_lowering = ty::ResolverAstLowering {
+ let ast_lowering = ty::ResolverAstLowering {
legacy_const_generic_args: self.legacy_const_generic_args.clone(),
partial_res_map: self.partial_res_map.clone(),
import_res_map: self.import_res_map.clone(),
@@ -1472,7 +1473,7 @@ impl<'a> Resolver<'a> {
trait_map: self.trait_map.clone(),
builtin_macro_kinds: self.builtin_macro_kinds.clone(),
};
- (definitions, cstore, resolutions, resolutions_lowering)
+ ResolverOutputs { definitions, global_ctxt, ast_lowering }
}
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
@@ -1520,8 +1521,8 @@ impl<'a> Resolver<'a> {
pub fn resolve_crate(&mut self, krate: &Crate) {
self.session.time("resolve_crate", || {
self.session.time("finalize_imports", || ImportResolver { r: self }.finalize_imports());
- self.session.time("resolve_access_levels", || {
- AccessLevelsVisitor::compute_access_levels(self, krate)
+ self.session.time("compute_effective_visibilities", || {
+ EffectiveVisibilitiesVisitor::compute_effective_visibilities(self, krate)
});
self.session.time("finalize_macro_resolutions", || self.finalize_macro_resolutions());
self.session.time("late_resolve_crate", || self.late_resolve_crate(krate));
@@ -1882,12 +1883,10 @@ impl<'a> Resolver<'a> {
match self.maybe_resolve_path(&segments, Some(ns), &parent_scope) {
PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()),
- PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
- Some(path_res.base_res())
+ PathResult::NonModule(path_res) => path_res.full_res(),
+ PathResult::Module(ModuleOrUniformRoot::ExternPrelude) | PathResult::Failed { .. } => {
+ None
}
- PathResult::Module(ModuleOrUniformRoot::ExternPrelude)
- | PathResult::NonModule(..)
- | PathResult::Failed { .. } => None,
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
}
}
@@ -1911,6 +1910,11 @@ impl<'a> Resolver<'a> {
}
}
+ /// For rustdoc.
+ pub fn get_partial_res(&self, node_id: NodeId) -> Option<PartialRes> {
+ self.partial_res_map.get(&node_id).copied()
+ }
+
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
#[inline]
pub fn opt_span(&self, def_id: DefId) -> Option<Span> {
@@ -1938,12 +1942,8 @@ impl<'a> Resolver<'a> {
return None;
}
- let partial_res = self.partial_res_map.get(&expr.id)?;
- if partial_res.unresolved_segments() != 0 {
- return None;
- }
-
- if let Res::Def(def::DefKind::Fn, def_id) = partial_res.base_res() {
+ let res = self.partial_res_map.get(&expr.id)?.full_res()?;
+ if let Res::Def(def::DefKind::Fn, def_id) = res {
// We only support cross-crate argument rewriting. Uses
// within the same crate should be updated to use the new
// const generics style.
@@ -2065,7 +2065,7 @@ struct Finalize {
/// Span of the whole path or some its characteristic fragment.
/// E.g. span of `b` in `foo::{a, b, c}`, or full span for regular paths.
path_span: Span,
- /// Span of the path start, suitable for prepending something to to it.
+ /// Span of the path start, suitable for prepending something to it.
/// E.g. span of `foo` in `foo::{a, b, c}`, or full span for regular paths.
root_span: Span,
/// Whether to report privacy errors or silently return "no resolution" for them,
@@ -2082,7 +2082,3 @@ impl Finalize {
Finalize { node_id, path_span, root_span, report_private: true }
}
}
-
-pub fn provide(providers: &mut Providers) {
- late::lifetimes::provide(providers);
-}
diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs
index dafa10e9e..9526296f9 100644
--- a/compiler/rustc_resolve/src/macros.rs
+++ b/compiler/rustc_resolve/src/macros.rs
@@ -12,7 +12,7 @@ use rustc_attr::StabilityLevel;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::Lrc;
-use rustc_errors::struct_span_err;
+use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand};
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::compile_declarative_macro;
@@ -590,9 +590,7 @@ impl<'a> Resolver<'a> {
let res = if path.len() > 1 {
let res = match self.maybe_resolve_path(&path, Some(MacroNS), parent_scope) {
- PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
- Ok(path_res.base_res())
- }
+ PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => Ok(res),
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
PathResult::NonModule(..)
| PathResult::Indeterminate
@@ -692,12 +690,23 @@ impl<'a> Resolver<'a> {
Some(Finalize::new(ast::CRATE_NODE_ID, path_span)),
None,
) {
- PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
- let res = path_res.base_res();
- check_consistency(self, &path, path_span, kind, initial_res, res);
+ PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => {
+ check_consistency(self, &path, path_span, kind, initial_res, res)
}
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => {
+ let mut suggestion = None;
let (span, label) = if let PathResult::Failed { span, label, .. } = path_res {
+ // try to suggest if it's not a macro, maybe a function
+ if let PathResult::NonModule(partial_res) = self.maybe_resolve_path(&path, Some(ValueNS), &parent_scope)
+ && partial_res.unresolved_segments() == 0 {
+ let sm = self.session.source_map();
+ let exclamation_span = sm.next_point(span);
+ suggestion = Some((
+ vec![(exclamation_span, "".to_string())],
+ format!("{} is not a macro, but a {}, try to remove `!`", Segment::names_to_string(&path), partial_res.base_res().descr()),
+ Applicability::MaybeIncorrect
+ ));
+ }
(span, label)
} else {
(
@@ -711,7 +720,7 @@ impl<'a> Resolver<'a> {
};
self.report_error(
span,
- ResolutionError::FailedToResolve { label, suggestion: None },
+ ResolutionError::FailedToResolve { label, suggestion },
);
}
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),