Skip to content
Merged
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
12 changes: 6 additions & 6 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
debug!(?alias_args);

ty::AliasTerm::new_from_def_id(
tcx,
assoc_item.def_id,
alias_args,
ty::AliasConstInherentArgsKind::WithSelf,
)
let kind = if let ty::AssocTag::Const = assoc_tag {
ty::AliasTermKind::ProjectionConst { def_id: assoc_item.def_id }
} else {
ty::AliasTermKind::ProjectionTy { def_id: assoc_item.def_id }
};
ty::AliasTerm::new_from_args(tcx, kind, alias_args)
})
};

Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&item_segment,
trait_ref.args,
);
ty::AliasTerm::new_from_def_id(
tcx,
assoc_item.def_id,
alias_args,
ty::AliasConstInherentArgsKind::WithSelf,
)
let kind = ty::AliasTermKind::ProjectionConst {
def_id: assoc_item.def_id,
};
ty::AliasTerm::new_from_args(tcx, kind, alias_args)
});

// FIXME(mgca): code duplication with other places we lower
Expand Down
32 changes: 12 additions & 20 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,12 +1608,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}

Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(
tcx,
item_def_id,
args,
ty::AliasConstInherentArgsKind::WithSelf,
)))
let kind = match mode {
LowerTypeRelativePathMode::Type(..) => {
ty::AliasTermKind::ProjectionTy { def_id: item_def_id }
}
LowerTypeRelativePathMode::Const => {
ty::AliasTermKind::ProjectionConst { def_id: item_def_id }
}
};

Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_args(tcx, kind, args)))
}

/// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.
Expand Down Expand Up @@ -1947,11 +1951,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
self.check_const_item_in_type_system(item_def_id, span)?;
let alias_const = ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
item_def_id,
ty::AliasConstInherentArgsKind::WithSelf,
),
ty::AliasConstKind::Projection { def_id: item_def_id },
item_args,
);
Ok(Const::new_alias(tcx, ty::IsRigid::No, alias_const))
Expand Down Expand Up @@ -2911,15 +2911,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
ty::Const::new_alias(
tcx,
ty::IsRigid::No,
ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
did,
ty::AliasConstInherentArgsKind::WithSelf,
),
args,
),
ty::AliasConst::new(tcx, ty::AliasConstKind::Free { def_id: did }, args),
)
}
Res::Def(kind @ DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
Expand Down
25 changes: 16 additions & 9 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{self, Debug, Display, Formatter};

use rustc_abi::{HasDataLayout, Size};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_macros::{Lift, StableHash, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
use rustc_span::{DUMMY_SP, RemapPathScopeComponents, Span, Symbol, bug};
Expand Down Expand Up @@ -478,15 +479,21 @@ impl<'tcx> UnevaluatedConst<'tcx> {
#[inline]
pub fn shrink(self, tcx: TyCtxt<'tcx>) -> ty::AliasConst<'tcx> {
assert_eq!(self.promoted, None);
ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
self.def,
ty::AliasConstInherentArgsKind::Impl,
),
self.args,
)

let kind = match tcx.def_kind(self.def) {
DefKind::AssocConst => {
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(self.def)) {
ty::AliasConstKind::InherentImpl { def_id: self.def }
} else {
ty::AliasConstKind::Projection { def_id: self.def }
}
}
DefKind::Const => ty::AliasConstKind::Free { def_id: self.def },
DefKind::AnonConst => ty::AliasConstKind::Anon { def_id: self.def },
kind => bug!("unexpected DefKind in MIR UnevaluatedConst: {kind:?}"),
};

ty::AliasConst::new(tcx, kind, self.args)
}
}

Expand Down
65 changes: 0 additions & 65 deletions compiler/rustc_middle/src/ty/context/impl_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,71 +220,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.adt_def(adt_def_id)
}

fn alias_const_kind_from_def_id(
self,
def_id: Self::DefId,
inherent_args: ty::AliasConstInherentArgsKind,
) -> ty::AliasConstKind<'tcx> {
match self.def_kind(def_id) {
DefKind::AssocConst => {
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) {
match inherent_args {
ty::AliasConstInherentArgsKind::WithSelf => {
ty::AliasConstKind::InherentSelf { def_id }
}
ty::AliasConstInherentArgsKind::Impl => {
ty::AliasConstKind::InherentImpl { def_id }
}
}
} else {
ty::AliasConstKind::Projection { def_id }
}
}
DefKind::Const => ty::AliasConstKind::Free { def_id },
DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => {
ty::AliasConstKind::Anon { def_id }
}
kind => bug!("unexpected DefKind in AliasConst: {kind:?}"),
}
}

fn alias_term_kind_from_def_id(
self,
def_id: DefId,
inherent_args: ty::AliasConstInherentArgsKind,
) -> ty::AliasTermKind<'tcx> {
match self.def_kind(def_id) {
DefKind::AssocTy => {
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) {
ty::AliasTermKind::InherentTy { def_id }
} else {
ty::AliasTermKind::ProjectionTy { def_id }
}
}
DefKind::AssocConst => {
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) {
match inherent_args {
ty::AliasConstInherentArgsKind::WithSelf => {
ty::AliasTermKind::InherentConstSelf { def_id }
}
ty::AliasConstInherentArgsKind::Impl => {
ty::AliasTermKind::InherentConstImpl { def_id }
}
}
} else {
ty::AliasTermKind::ProjectionConst { def_id }
}
}
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy { def_id },
DefKind::TyAlias => ty::AliasTermKind::FreeTy { def_id },
DefKind::Const => ty::AliasTermKind::FreeConst { def_id },
DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => {
ty::AliasTermKind::AnonConst { def_id }
}
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
}
}

fn trait_ref_and_own_args_for_alias(
self,
def_id: DefId,
Expand Down
30 changes: 17 additions & 13 deletions compiler/rustc_mir_build/src/builder/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,37 @@ pub(crate) fn as_constant_inner<'tcx>(
}
ExprKind::NamedConst { def_id, args, ref user_ty } => {
let user_ty = user_ty.as_ref().and_then(push_cuta);

// Under generic_const_args, `def_id` might be a regular const declared in a trait, but
// is `impl`d as a directly represented const. We do not know whether it is here, so we
// must use type system normalization for all consts under generic_const_args.
Comment on lines 75 to 79

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we reword this?

// FIXME(generic_const_args): there's a lot to consider here! `Const::Ty` uses valtrees
// and `Const::Unevaluated` does not, we should revisit this before stabilization.
let def_kind = tcx.def_kind(def_id);
if tcx.features().generic_const_args()
|| matches!(tcx.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
|| matches!(def_kind, DefKind::Const | DefKind::AssocConst)
&& tcx.is_direct_const(def_id)
{
let uneval = ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
def_id,
ty::AliasConstInherentArgsKind::Impl,
),
args,
);
let ct = ty::Const::new_alias(tcx, ty::IsRigid::No, uneval);

let kind = match def_kind {
DefKind::AssocConst => {
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(def_id))
{
ty::AliasConstKind::InherentImpl { def_id }
} else {
ty::AliasConstKind::Projection { def_id }
}
}
DefKind::Const => ty::AliasConstKind::Free { def_id },
_ => unreachable!(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

reachable via #162923

Its always better here to print the actual Defkind or whatever we are not expecting to be reachable as this makes categorizing the ICE a bit easier imho :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

aaaah sorry!! #162937

};
let alias = ty::AliasConst::new(tcx, kind, args);
let ct = ty::Const::new_alias(tcx, ty::IsRigid::No, alias);
let const_ = Const::Ty(ty, ct);
return ConstOperand { span, user_ty, const_ };
}

let uneval = mir::UnevaluatedConst::new(def_id, args);
let const_ = Const::Unevaluated(uneval, ty);

ConstOperand { user_ty, span, const_ }
}
ExprKind::ConstParam { param, def_id: _ } => {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
} else if let hir::ExprKind::Path(ref qpath) = source.kind
&& let res = self.typeck_results.qpath_res(qpath, source.hir_id)
&& let ty = self.typeck_results.node_type(source.hir_id)
&& let ty::Adt(adt_def, args) = ty.kind()
&& let ty::Adt(adt_def, _) = ty.kind()
Comment thread
bit-aloo marked this conversation as resolved.
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), variant_ctor_id) = res
{
// Check whether this is casting an enum variant discriminant.
Expand Down Expand Up @@ -369,6 +369,8 @@ impl<'tcx> ThirBuildCx<'tcx> {
// in case we are offsetting from a computed discriminant
// and not the beginning of discriminants (which is always `0`)
Some(did) => {
let args = self.tcx.mk_args(&[]);
self.tcx.debug_assert_args_compatible(did, args);
let kind = ExprKind::NamedConst { def_id: did, args, user_ty: None };
let lhs =
self.thir.exprs.push(Expr { temp_scope_id, ty: discr_ty, span, kind });
Expand Down
33 changes: 14 additions & 19 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,17 @@ impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> {
let ty = self.typeck_results.node_type(id);
let res = self.typeck_results.qpath_res(qpath, id);

let (def_id, user_ty) = match res {
Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
(def_id, self.typeck_results.user_provided_types().get(id))
let kind = match res {
Res::Def(DefKind::Const, def_id) => ty::AliasConstKind::Free { def_id },

Res::Def(DefKind::AssocConst, def_id) => {
if let DefKind::Impl { of_trait: false } =
self.tcx.def_kind(self.tcx.parent(def_id))
{
ty::AliasConstKind::InherentImpl { def_id }
} else {
ty::AliasConstKind::Projection { def_id }
}
}

_ => {
Expand All @@ -649,26 +657,13 @@ impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> {

// Lower the named constant to a THIR pattern.
let args = self.typeck_results.node_args(id);
// FIXME(mgca): we will need to special case IACs here to have type system compatible
// generic args, instead of how we represent them in body expressions.
let c = ty::Const::new_alias(
self.tcx,
ty::IsRigid::No,
ty::AliasConst::new(
self.tcx,
ty::AliasConstKind::new_from_def_id(
self.tcx,
def_id,
ty::AliasConstInherentArgsKind::Impl,
),
args,
),
);
let alias = ty::AliasConst::new(self.tcx, kind, args);
let c = ty::Const::new_alias(self.tcx, ty::IsRigid::No, alias);
let mut pattern = self.const_to_pat(c, ty, id, span);

// If this is an associated constant with an explicit user-written
// type, add an ascription node (e.g. `<Foo<'a> as MyTrait>::CONST`).
if let Some(&user_ty) = user_ty {
if let Some(&user_ty) = self.typeck_results.user_provided_types().get(id) {
let annotation = CanonicalUserTypeAnnotation {
user_ty: Box::new(user_ty),
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,23 @@ fn trait_object_ty<'tcx>(tcx: TyCtxt<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tc
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
.map(move |assoc_item| {
super_poly_trait_ref.map_bound(|super_trait_ref| {
let projection_term = ty::AliasTerm::new_from_def_id(
tcx,
assoc_item.def_id,
super_trait_ref.args,
ty::AliasConstInherentArgsKind::WithSelf,
);
let term = tcx.normalize_erasing_regions(
let kind = if assoc_item.is_type() {
ty::AliasTermKind::ProjectionTy { def_id: assoc_item.def_id }
} else {
ty::AliasTermKind::ProjectionConst { def_id: assoc_item.def_id }
};
let projection_term =
ty::AliasTerm::new_from_args(tcx, kind, super_trait_ref.args);
let term = projection_term.to_term(tcx, ty::IsRigid::No);
let normalized_term = tcx.normalize_erasing_regions(
ty::TypingEnv::fully_monomorphized(),
Unnormalized::new_wip(projection_term.to_term(tcx, ty::IsRigid::No)),
);
debug!(
"Projection {:?} -> {term}",
projection_term.to_term(tcx, ty::IsRigid::No)
Unnormalized::new_wip(term),
);
debug!("Projection {term} -> {normalized_term}");
ty::ExistentialPredicate::Projection(
ty::ExistentialProjection::erase_self_ty(
tcx,
ty::ProjectionClause { projection_term, term },
ty::ProjectionClause { projection_term, term: normalized_term },
),
)
})
Expand Down
27 changes: 16 additions & 11 deletions compiler/rustc_ty_utils/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::thir::visit::Visitor;
use rustc_middle::ty::abstract_const::CastKind;
use rustc_middle::ty::{self, Expr, LitToConstInput, TyCtxt, TypeVisitableExt};
use rustc_middle::{mir, thir};
use rustc_span::Span;
use rustc_span::{Span, bug};
use tracing::instrument;

use crate::diagnostics::{GenericConstantTooComplex, GenericConstantTooComplexSub};
Expand Down Expand Up @@ -70,16 +70,21 @@ fn recurse_build<'tcx>(
}
&ExprKind::ZstLiteral { user_ty: _ } => ty::Const::zero_sized(tcx, node.ty),
&ExprKind::NamedConst { def_id, args, user_ty: _ } => {
let uneval = ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
def_id,
ty::AliasConstInherentArgsKind::Impl,
),
args,
);
ty::Const::new_alias(tcx, ty::IsRigid::No, uneval)
let kind = match tcx.def_kind(def_id) {
DefKind::AssocConst => {
if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(def_id)) {
ty::AliasConstKind::InherentImpl { def_id }
} else {
ty::AliasConstKind::Projection { def_id }
}
}
DefKind::Const => ty::AliasConstKind::Free { def_id },
DefKind::AnonConst => ty::AliasConstKind::Anon { def_id },
kind => bug!("unexpected DefKind in THIR ExprKind::NamedConst: {kind:?}"),
};

let alias = ty::AliasConst::new(tcx, kind, args);
ty::Const::new_alias(tcx, ty::IsRigid::No, alias)
}
ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param),

Expand Down
Loading
Loading