Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
// `a::b::Trait(Args)::TraitItem`
Res::Def(DefKind::AssocFn, _)
| Res::Def(DefKind::AssocConst, _)
| Res::Def(DefKind::AssocConst { .. }, _)
| Res::Def(DefKind::AssocTy, _)
if i + 2 == proj_start =>
{
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = base.kind else { return };
let (hir::def::Res::Local(_)
| hir::def::Res::Def(
DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } | DefKind::AssocConst,
DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::Static { .. }
| DefKind::AssocConst { .. },
_,
)) = path.res
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
// - We must compute the normalized signature and then compute implied bounds from that
// in order to connect any unconstrained region vars created during normalization to
// the types of the locals corresponding to the inputs and outputs of the item. (#136547)
if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst) {
if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst { .. })
{
for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) {
let result: Result<_, ErrorGuaranteed> = param_env
.and(DeeplyNormalize { value: ty })
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ fn setup_for_eval<'tcx>(
cid.promoted.is_some()
|| matches!(
ecx.tcx.def_kind(cid.instance.def_id()),
DefKind::Const
DefKind::Const { .. }
| DefKind::Static { .. }
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst
| DefKind::AssocConst { .. }
),
"Unexpected DefKind: {:?}",
ecx.tcx.def_kind(cid.instance.def_id())
Expand Down
36 changes: 20 additions & 16 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ pub enum DefKind {

// Value namespace
Fn,
Const,
Const {
is_type_const: bool,
},
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
Static {
Expand All @@ -147,7 +149,9 @@ pub enum DefKind {
/// or `trait Foo { fn associated() {} }`
AssocFn,
/// Associated constant: `trait MyTrait { const ASSOC: usize; }`
AssocConst,
AssocConst {
is_type_const: bool,
},

// Macro namespace
Macro(MacroKinds),
Expand Down Expand Up @@ -222,8 +226,8 @@ impl DefKind {
DefKind::Trait => "trait",
DefKind::ForeignTy => "foreign type",
DefKind::AssocFn => "associated function",
DefKind::Const => "constant",
DefKind::AssocConst => "associated constant",
DefKind::Const { .. } => "constant",
DefKind::AssocConst { .. } => "associated constant",
DefKind::TyParam => "type parameter",
DefKind::ConstParam => "const parameter",
DefKind::Macro(kinds) => kinds.descr(),
Expand All @@ -249,7 +253,7 @@ impl DefKind {
pub fn article(&self) -> &'static str {
match *self {
DefKind::AssocTy
| DefKind::AssocConst
| DefKind::AssocConst { .. }
| DefKind::AssocFn
| DefKind::Enum
| DefKind::OpaqueTy
Expand Down Expand Up @@ -277,12 +281,12 @@ impl DefKind {
| DefKind::TyParam => Some(Namespace::TypeNS),

DefKind::Fn
| DefKind::Const
| DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::Static { .. }
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst => Some(Namespace::ValueNS),
| DefKind::AssocConst { .. } => Some(Namespace::ValueNS),

DefKind::Macro(..) => Some(Namespace::MacroNS),

Expand Down Expand Up @@ -323,11 +327,11 @@ impl DefKind {
DefKind::AssocTy => DefPathData::TypeNs(name.unwrap()),

DefKind::Fn
| DefKind::Const
| DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::Static { .. }
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::AssocConst { .. }
| DefKind::Field => DefPathData::ValueNs(name.unwrap()),
DefKind::Macro(..) => DefPathData::MacroNs(name.unwrap()),
DefKind::LifetimeParam => DefPathData::LifetimeNs(name.unwrap()),
Expand All @@ -345,7 +349,7 @@ impl DefKind {
}

pub fn is_assoc(self) -> bool {
matches!(self, DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy)
matches!(self, DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy)
}

/// This is a "module" in name resolution sense.
Expand All @@ -371,11 +375,11 @@ impl DefKind {
pub fn has_generics(self) -> bool {
match self {
DefKind::AnonConst
| DefKind::AssocConst
| DefKind::AssocConst { .. }
| DefKind::AssocFn
| DefKind::AssocTy
| DefKind::Closure
| DefKind::Const
| DefKind::Const { .. }
| DefKind::Ctor(..)
| DefKind::Enum
| DefKind::Field
Expand Down Expand Up @@ -423,8 +427,8 @@ impl DefKind {
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::AssocTy
| DefKind::Const
| DefKind::AssocConst
| DefKind::Const { .. }
| DefKind::AssocConst { .. }
| DefKind::Macro(..)
| DefKind::Use
| DefKind::ForeignMod
Expand Down Expand Up @@ -459,12 +463,12 @@ impl DefKind {
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::Fn
| DefKind::Const
| DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::Static { .. }
| DefKind::Ctor(_, _)
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::AssocConst { .. }
| DefKind::Macro(_)
| DefKind::ExternCrate
| DefKind::Use
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl Target {
DefKind::ExternCrate => Target::ExternCrate,
DefKind::Use => Target::Use,
DefKind::Static { .. } => Target::Static,
DefKind::Const => Target::Const,
DefKind::Const { .. } => Target::Const,
DefKind::Fn => Target::Fn,
DefKind::Macro(..) => Target::MacroDef,
DefKind::Mod => Target::Mod,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
// avoids this query from having a direct dependency edge on the HIR
return res;
}
DefKind::Const => {
DefKind::Const { .. } => {
tcx.ensure_ok().generics_of(def_id);
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
Expand Down Expand Up @@ -1062,7 +1062,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
// avoids this query from having a direct dependency edge on the HIR
return res;
}
DefKind::AssocConst => {
DefKind::AssocConst { .. } => {
tcx.ensure_ok().type_of(def_id);
tcx.ensure_ok().predicates_of(def_id);
res = res.and(check_associated_item(tcx, def_id));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2360,11 +2360,11 @@ fn lint_redundant_lifetimes<'tcx>(
| DefKind::Trait
| DefKind::TraitAlias
| DefKind::Fn
| DefKind::Const
| DefKind::Const { .. }
| DefKind::Impl { of_trait: _ } => {
// Proceed
}
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst { .. } => {
if tcx.trait_impl_of_assoc(owner_id.to_def_id()).is_some() {
// Don't check for redundant lifetimes for associated items of trait
// implementations, since the signature is required to be compatible
Expand Down
20 changes: 0 additions & 20 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub(crate) fn provide(providers: &mut Providers) {
const_param_default,
anon_const_kind,
const_of_item,
is_rhs_type_const,
..*providers
};
}
Expand Down Expand Up @@ -1702,22 +1701,3 @@ fn const_of_item<'tcx>(
ty::EarlyBinder::bind(ct)
}
}

/// Check if a Const or AssocConst is a type const (mgca)
fn is_rhs_type_const<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> bool {
match tcx.hir_node_by_def_id(def) {
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Const(_, _, _, hir::ConstItemRhs::TypeConst(_)),
..
})
| hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Const(_, hir::ConstItemRhs::TypeConst(_)),
..
})
| hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Const(_, _, hir::IsTypeConst::Yes),
..
}) => return true,
_ => return false,
}
}
15 changes: 8 additions & 7 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ impl LowerTypeRelativePathMode {
}
}

fn def_kind(self) -> DefKind {
///NOTE: use `assoc_tag` for any important logic
fn def_kind_for_diagnostics(self) -> DefKind {
match self {
Self::Type(_) => DefKind::AssocTy,
Self::Const => DefKind::AssocConst,
Self::Const => DefKind::AssocConst { is_type_const: false },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in theory returning false here is wrong 🤔 but in practice def_kind is only ever used for diagnostics so I think its actually OK.

Could you rename this method to def_kind_for_diagnostics and add a comment saying to use assoc_tag for important logic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done that, but isn't is used in lower_type_relative_path?

}
}

Expand Down Expand Up @@ -1547,7 +1548,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
};

could_refer_to(DefKind::Variant, variant_def_id, "");
could_refer_to(mode.def_kind(), item_def_id, " also");
could_refer_to(mode.def_kind_for_diagnostics(), item_def_id, " also");

lint.span_suggestion(
span,
Expand Down Expand Up @@ -2080,12 +2081,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}

// Case 3. Reference to a top-level value.
DefKind::Fn | DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } => {
DefKind::Fn | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } => {
generic_segments.push(GenericPathSegment(def_id, last));
}

// Case 4. Reference to a method or associated const.
DefKind::AssocFn | DefKind::AssocConst => {
DefKind::AssocFn | DefKind::AssocConst { .. } => {
if segments.len() >= 2 {
let generics = tcx.generics_of(def_id);
generic_segments.push(GenericPathSegment(generics.parent.unwrap(), last - 1));
Expand Down Expand Up @@ -2658,7 +2659,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
self.lower_const_param(def_id, hir_id)
}
Res::Def(DefKind::Const, did) => {
Res::Def(DefKind::Const { .. }, did) => {
if let Err(guar) = self.require_type_const_attribute(did, span) {
return Const::new_error(self.tcx(), guar);
}
Expand Down Expand Up @@ -2699,7 +2700,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let args = self.lower_generic_args_of_path_segment(span, generics_did, segment);
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
}
Res::Def(DefKind::AssocConst, did) => {
Res::Def(DefKind::AssocConst { .. }, did) => {
let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None);
Some(trait_)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
if self.depth >= self.cause_depth {
self.cause = Some(error.obligation.cause);
if let hir::TyKind::TraitObject(..) = ty.kind
&& let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn =
&& let DefKind::AssocTy | DefKind::AssocConst { .. } | DefKind::AssocFn =
self.tcx.def_kind(self.def_id)
{
self.cause = Some(ObligationCause::new(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
tcx.ensure_ok().eval_static_initializer(item_def_id);
check::maybe_check_static_with_link_section(tcx, item_def_id);
}
DefKind::Const
DefKind::Const { .. }
if !tcx.generics_of(item_def_id).own_requires_monomorphization()
&& !tcx.is_type_const(item_def_id) =>
{
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Path {
res:
hir::def::Res::Def(
hir::def::DefKind::Static { .. } | hir::def::DefKind::Const,
hir::def::DefKind::Static { .. }
| hir::def::DefKind::Const { .. },
def_id,
),
..
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx

let res = self.cx.typeck_results().qpath_res(qpath, *hir_id);
match res {
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => {
Res::Def(DefKind::Const { .. }, _)
| Res::Def(DefKind::AssocConst { .. }, _) => {
// Named constants have to be equated with the value
// being matched, so that's a read of the value being matched.
//
Expand Down Expand Up @@ -1405,9 +1406,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
match res {
Res::Def(
DefKind::Ctor(..)
| DefKind::Const
| DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::AssocConst
| DefKind::AssocConst { .. }
| DefKind::Fn
| DefKind::AssocFn,
_,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) => {
err_extend = GenericsArgsErrExtend::DefVariant(segments);
}
Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => {
Res::Def(DefKind::AssocFn | DefKind::AssocConst { .. }, def_id) => {
let assoc_item = tcx.associated_item(def_id);
let container = assoc_item.container;
let container_id = assoc_item.container_id(tcx);
Expand Down Expand Up @@ -1314,7 +1314,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
});

let args_for_user_type = if let Res::Def(DefKind::AssocConst, def_id) = res {
let args_for_user_type = if let Res::Def(DefKind::AssocConst { .. }, def_id) = res {
self.transform_args_for_inherent_type_const(def_id, args_raw)
} else {
args_raw
Expand Down Expand Up @@ -1362,7 +1362,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_instantiated);

let args = if let Res::Def(DefKind::AssocConst, def_id) = res {
let args = if let Res::Def(DefKind::AssocConst { .. }, def_id) = res {
self.transform_args_for_inherent_type_const(def_id, args)
} else {
args
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::ExprKind::ConstBlock(..) => return None,
hir::ExprKind::Path(qpath) => {
let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id);
if let Res::Def(DefKind::Const | DefKind::AssocConst, _) = res {
if let Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _) = res
{
return None;
}
}
Expand Down
Loading
Loading