summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_resolve/src/ident.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /compiler/rustc_resolve/src/ident.rs
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_resolve/src/ident.rs')
-rw-r--r--compiler/rustc_resolve/src/ident.rs202
1 files changed, 114 insertions, 88 deletions
diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs
index 5a56d7b99..945c7ce3a 100644
--- a/compiler/rustc_resolve/src/ident.rs
+++ b/compiler/rustc_resolve/src/ident.rs
@@ -13,10 +13,12 @@ use rustc_span::{Span, DUMMY_SP};
use std::ptr;
+use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
use crate::late::{
- ConstantHasGenerics, ConstantItemKind, HasGenericParams, PathSource, Rib, RibKind,
+ ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
};
use crate::macros::{sub_namespace_match, MacroRulesScope};
+use crate::BindingKey;
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
@@ -24,7 +26,6 @@ use crate::{ResolutionError, Resolver, Scope, ScopeSet, Segment, ToNameBinding,
use Determinacy::*;
use Namespace::*;
-use RibKind::*;
type Visibility = ty::Visibility<LocalDefId>;
@@ -324,8 +325,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
module = match ribs[i].kind {
- ModuleRibKind(module) => module,
- MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
+ RibKind::Module(module) => module,
+ RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
ident.span.remove_mark();
@@ -527,7 +528,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
lint_id,
orig_ident.span,
- &format!(
+ format!(
"cannot find {} `{}` in this scope",
ns.descr(),
ident
@@ -865,7 +866,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
};
- let key = self.new_key(ident, ns);
+ let key = BindingKey::new(ident, ns);
let resolution =
self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
@@ -1084,7 +1085,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let ribs = &all_ribs[rib_index + 1..];
// An invalid forward use of a generic parameter from a previous default.
- if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
+ if let RibKind::ForwardGenericParamBan = all_ribs[rib_index].kind {
if let Some(span) = finalize {
let res_error = if rib_ident.name == kw::SelfUpper {
ResolutionError::SelfInGenericParamDefault
@@ -1104,14 +1105,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
for rib in ribs {
match rib.kind {
- NormalRibKind
- | ClosureOrAsyncRibKind
- | ModuleRibKind(..)
- | MacroDefinition(..)
- | ForwardGenericParamBanRibKind => {
+ RibKind::Normal
+ | RibKind::ClosureOrAsync
+ | RibKind::Module(..)
+ | RibKind::MacroDefinition(..)
+ | RibKind::ForwardGenericParamBan => {
// Nothing to do. Continue.
}
- ItemRibKind(_) | AssocItemRibKind => {
+ RibKind::Item(_) | RibKind::AssocItem => {
// This was an attempt to access an upvar inside a
// named function item. This is not allowed, so we
// report an error.
@@ -1123,42 +1124,45 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
}
}
- ConstantItemRibKind(_, item) => {
+ RibKind::ConstantItem(_, item) => {
// Still doesn't deal with upvars
if let Some(span) = finalize {
- let (span, resolution_error) =
- if let Some((ident, constant_item_kind)) = item {
- let kind_str = match constant_item_kind {
- ConstantItemKind::Const => "const",
- ConstantItemKind::Static => "static",
- };
- (
- span,
- AttemptToUseNonConstantValueInConstant(
- ident, "let", kind_str,
- ),
- )
- } else {
- (
- rib_ident.span,
- AttemptToUseNonConstantValueInConstant(
- original_rib_ident_def,
- "const",
- "let",
- ),
- )
- };
+ let (span, resolution_error) = match item {
+ None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
+ None => (
+ rib_ident.span,
+ AttemptToUseNonConstantValueInConstant(
+ original_rib_ident_def,
+ "const",
+ "let",
+ ),
+ ),
+ Some((ident, kind)) => (
+ span,
+ AttemptToUseNonConstantValueInConstant(
+ ident,
+ "let",
+ kind.as_str(),
+ ),
+ ),
+ };
self.report_error(span, resolution_error);
}
return Res::Err;
}
- ConstParamTyRibKind => {
+ RibKind::ConstParamTy => {
if let Some(span) = finalize {
- self.report_error(span, ParamInTyOfConstParam(rib_ident.name));
+ self.report_error(
+ span,
+ ParamInTyOfConstParam {
+ name: rib_ident.name,
+ param_kind: None,
+ },
+ );
}
return Res::Err;
}
- InlineAsmSymRibKind => {
+ RibKind::InlineAsmSym => {
if let Some(span) = finalize {
self.report_error(span, InvalidAsmSym);
}
@@ -1174,23 +1178,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
for rib in ribs {
let has_generic_params: HasGenericParams = match rib.kind {
- NormalRibKind
- | ClosureOrAsyncRibKind
- | ModuleRibKind(..)
- | MacroDefinition(..)
- | InlineAsmSymRibKind
- | AssocItemRibKind
- | ForwardGenericParamBanRibKind => {
+ RibKind::Normal
+ | RibKind::ClosureOrAsync
+ | RibKind::Module(..)
+ | RibKind::MacroDefinition(..)
+ | RibKind::InlineAsmSym
+ | RibKind::AssocItem
+ | RibKind::ForwardGenericParamBan => {
// Nothing to do. Continue.
continue;
}
- ConstantItemRibKind(trivial, _) => {
- let features = self.tcx.sess.features_untracked();
- // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
- if !(trivial == ConstantHasGenerics::Yes
- || features.generic_const_exprs)
- {
+ RibKind::ConstantItem(trivial, _) => {
+ if let ConstantHasGenerics::No(cause) = trivial {
// 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
@@ -1208,13 +1208,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
} else {
if let Some(span) = finalize {
- self.report_error(
- span,
- ResolutionError::ParamInNonTrivialAnonConst {
- name: rib_ident.name,
- is_type: true,
- },
- );
+ let error = match cause {
+ NoConstantGenericsReason::IsEnumDiscriminant => {
+ ResolutionError::ParamInEnumDiscriminant {
+ name: rib_ident.name,
+ param_kind: ParamKindInEnumDiscriminant::Type,
+ }
+ }
+ NoConstantGenericsReason::NonTrivialConstArg => {
+ ResolutionError::ParamInNonTrivialAnonConst {
+ name: rib_ident.name,
+ param_kind:
+ ParamKindInNonTrivialAnonConst::Type,
+ }
+ }
+ };
+ self.report_error(span, error);
self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
}
@@ -1226,12 +1235,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
// This was an attempt to use a type parameter outside its scope.
- ItemRibKind(has_generic_params) => has_generic_params,
- ConstParamTyRibKind => {
+ RibKind::Item(has_generic_params) => has_generic_params,
+ RibKind::ConstParamTy => {
if let Some(span) = finalize {
self.report_error(
span,
- ResolutionError::ParamInTyOfConstParam(rib_ident.name),
+ ResolutionError::ParamInTyOfConstParam {
+ name: rib_ident.name,
+ param_kind: Some(errors::ParamKindInTyOfConstParam::Type),
+ },
);
}
return Res::Err;
@@ -1253,29 +1265,34 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Res::Def(DefKind::ConstParam, _) => {
for rib in ribs {
let has_generic_params = match rib.kind {
- NormalRibKind
- | ClosureOrAsyncRibKind
- | ModuleRibKind(..)
- | MacroDefinition(..)
- | InlineAsmSymRibKind
- | AssocItemRibKind
- | ForwardGenericParamBanRibKind => continue,
-
- ConstantItemRibKind(trivial, _) => {
- let features = self.tcx.sess.features_untracked();
- // HACK(min_const_generics): We currently only allow `N` or `{ N }`.
- if !(trivial == ConstantHasGenerics::Yes
- || features.generic_const_exprs)
- {
+ RibKind::Normal
+ | RibKind::ClosureOrAsync
+ | RibKind::Module(..)
+ | RibKind::MacroDefinition(..)
+ | RibKind::InlineAsmSym
+ | RibKind::AssocItem
+ | RibKind::ForwardGenericParamBan => continue,
+
+ RibKind::ConstantItem(trivial, _) => {
+ if let ConstantHasGenerics::No(cause) = trivial {
if let Some(span) = finalize {
- self.report_error(
- span,
- ResolutionError::ParamInNonTrivialAnonConst {
- name: rib_ident.name,
- is_type: false,
- },
- );
- self.tcx.sess.delay_span_bug(span, CG_BUG_STR);
+ let error = match cause {
+ NoConstantGenericsReason::IsEnumDiscriminant => {
+ ResolutionError::ParamInEnumDiscriminant {
+ name: rib_ident.name,
+ param_kind: ParamKindInEnumDiscriminant::Const,
+ }
+ }
+ NoConstantGenericsReason::NonTrivialConstArg => {
+ ResolutionError::ParamInNonTrivialAnonConst {
+ name: rib_ident.name,
+ param_kind: ParamKindInNonTrivialAnonConst::Const {
+ name: rib_ident.name,
+ },
+ }
+ }
+ };
+ self.report_error(span, error);
}
return Res::Err;
@@ -1284,12 +1301,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
continue;
}
- ItemRibKind(has_generic_params) => has_generic_params,
- ConstParamTyRibKind => {
+ RibKind::Item(has_generic_params) => has_generic_params,
+ RibKind::ConstParamTy => {
if let Some(span) = finalize {
self.report_error(
span,
- ResolutionError::ParamInTyOfConstParam(rib_ident.name),
+ ResolutionError::ParamInTyOfConstParam {
+ name: rib_ident.name,
+ param_kind: Some(errors::ParamKindInTyOfConstParam::Const),
+ },
);
}
return Res::Err;
@@ -1345,7 +1365,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ribs: Option<&PerNS<Vec<Rib<'a>>>>,
ignore_binding: Option<&'a NameBinding<'a>>,
) -> PathResult<'a> {
- debug!("resolve_path(path={:?}, opt_ns={:?}, finalize={:?})", path, opt_ns, finalize);
+ debug!(
+ "resolve_path(path={:?}, opt_ns={:?}, finalize={:?}) path_len: {}",
+ path,
+ opt_ns,
+ finalize,
+ path.len()
+ );
let mut module = None;
let mut allow_super = true;