Skip to content
Closed
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
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
Const::Ty(_, ct) => match ct.kind() {
ty::ConstKind::Alias(_, alias_const) => match alias_const.kind {
ty::AliasConstKind::Projection { def_id }
| ty::AliasConstKind::Inherent { def_id }
| ty::AliasConstKind::InherentSelf { def_id }
| ty::AliasConstKind::InherentImpl { def_id }
| ty::AliasConstKind::Free { def_id }
| ty::AliasConstKind::Anon { def_id } => Some(UnevaluatedConst {
def: def_id,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ fn list_metadata(sess: &Session, metadata_loader: &dyn MetadataLoader) {
}
}

fn print_crate_info(
pub fn print_crate_info(
codegen_backend: &dyn CodegenBackend,
sess: &Session,
parse_attrs: bool,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2727,9 +2727,9 @@ fn param_env_with_gat_bounds<'tcx>(
_ => clauses.push(
ty::Binder::bind_with_vars(
ty::ProjectionClause {
projection_term: ty::AliasTerm::new_from_def_id(
projection_term: ty::AliasTerm::new(
tcx,
trait_ty.def_id,
ty::AliasTermKind::ProjectionTy { def_id: trait_ty.def_id },
rebased_args,
),
term: normalize_impl_ty.into(),
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
debug!(?alias_args);

ty::AliasTerm::new_from_def_id(tcx, assoc_item.def_id, alias_args)
ty::AliasTerm::new_from_def_id(
tcx,
assoc_item.def_id,
alias_args,
ty::AliasConstInherentArgsKind::WithSelf,
)
})
};

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
tcx,
assoc_item.def_id,
alias_args,
ty::AliasConstInherentArgsKind::WithSelf,
)
});

Expand Down
39 changes: 22 additions & 17 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}

Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(tcx, item_def_id, args)))
Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(
tcx,
item_def_id,
args,
ty::AliasConstInherentArgsKind::WithSelf,
)))
}

/// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.
Expand Down Expand Up @@ -1773,12 +1778,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

let kind = match assoc_tag {
ty::AssocTag::Type => ty::AliasTermKind::InherentTy { def_id: assoc_item },
ty::AssocTag::Const => {
// FIXME(mgca): drop once `InherentConst` accepts IAC-shaped args (issue #156181)
// without this, `new_from_args` errors (#155341).
self.require_type_const_attribute(assoc_item, span)?;
ty::AliasTermKind::InherentConst { def_id: assoc_item }
}
ty::AssocTag::Const => ty::AliasTermKind::InherentConstSelf { def_id: assoc_item },
ty::AssocTag::Fn => unreachable!(),
};

Expand Down Expand Up @@ -1948,7 +1948,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
self.require_type_const_attribute(item_def_id, span)?;
let alias_const = ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(tcx, item_def_id),
ty::AliasConstKind::new_from_def_id(
tcx,
item_def_id,
ty::AliasConstInherentArgsKind::WithSelf,
),
item_args,
);
Ok(Const::new_alias(tcx, ty::IsRigid::No, alias_const))
Expand Down Expand Up @@ -2903,7 +2907,15 @@ 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), args),
ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
did,
ty::AliasConstInherentArgsKind::WithSelf,
),
args,
),
)
}
Res::Def(kind @ DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
Expand Down Expand Up @@ -3141,14 +3153,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
span: Span,
) -> Result<(), ErrorGuaranteed> {
let tcx = self.tcx();
// FIXME(gca): Intentionally disallowing paths to inherent associated non-type constants
// until a refactoring for how generic args for IACs are represented has been landed.
let is_inherent_assoc_const = tcx.def_kind(def_id)
== DefKind::AssocConst { is_type_const: false }
&& tcx.def_kind(tcx.parent(def_id)) == DefKind::Impl { of_trait: false };
if tcx.is_type_const(def_id)
|| tcx.features().generic_const_args() && !is_inherent_assoc_const
{
if tcx.is_type_const(def_id) || tcx.features().generic_const_args() {
Ok(())
} else {
let mut err = self.dcx().struct_span_err(
Expand Down
48 changes: 2 additions & 46 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,6 @@ use crate::method::{self, MethodCallee};
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Transform generic args for inherent associated type constants (IACs).
///
/// IACs have a different generic parameter structure than regular associated constants:
/// - Regular assoc const: parent (impl) generic params + own generic params
/// - IAC (type_const): Self type + own generic params
pub(crate) fn transform_args_for_inherent_type_const(
&self,
def_id: DefId,
args: GenericArgsRef<'tcx>,
) -> GenericArgsRef<'tcx> {
let tcx = self.tcx;
if !tcx.is_type_const(def_id) {
return args;
}
let Some(assoc_item) = tcx.opt_associated_item(def_id) else {
return args;
};
if !matches!(assoc_item.container, ty::AssocContainer::InherentImpl) {
return args;
}

let impl_def_id = assoc_item.container_id(tcx);
let generics = tcx.generics_of(def_id);
let impl_args = &args[..generics.parent_count];
let self_ty = tcx.type_of(impl_def_id).instantiate(tcx, impl_args).skip_norm_wip();
// Build new args: [Self, own_args...]
let own_args = &args[generics.parent_count..];
tcx.mk_args_from_iter(
std::iter::once(ty::GenericArg::from(self_ty)).chain(own_args.iter().copied()),
)
}

/// Produces warning on the given node, if the current point in the
/// function is unreachable, and there hasn't been another warning.
pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
Expand Down Expand Up @@ -1399,7 +1367,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

let args_raw = implicit_args.unwrap_or_else(|| {
let args_for_user_type = implicit_args.unwrap_or_else(|| {
lower_generic_args(
self,
def_id,
Expand All @@ -1417,17 +1385,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
});

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
};

// First, store the "user args" for later.
self.write_user_type_annotation_from_args(hir_id, def_id, args_for_user_type, user_self_ty);

// Normalize only after registering type annotations.
let args = self.normalize(span, Unnormalized::new_wip(args_raw));
let args = self.normalize(span, Unnormalized::new_wip(args_for_user_type));

self.add_required_obligations_for_hir(span, def_id, args, hir_id);

Expand Down Expand Up @@ -1465,12 +1427,6 @@ 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 {
self.transform_args_for_inherent_type_const(def_id, args)
} else {
args
};

self.write_args(hir_id, args);

(ty_instantiated, res)
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
impl_def_id,
impl_trait_ref.args,
);
tcx.check_args_compatible(trait_item_def_id, args)
let alias_kind = ty::AliasTermKind::ProjectionConst { def_id: trait_item_def_id };
tcx.check_alias_term_args_compatible(alias_kind, args)
.then(|| tcx.type_of(trait_item_def_id).instantiate(tcx, args).skip_norm_wip())
} else {
Some(fcx.next_ty_var(span))
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ impl<'tcx> InferCtxt<'tcx> {
| ty::AliasTermKind::OpaqueTy { .. }
| ty::AliasTermKind::FreeTy { .. } => self.next_ty_var(span).into(),
ty::AliasTermKind::FreeConst { .. }
| ty::AliasTermKind::InherentConst { .. }
| ty::AliasTermKind::InherentConstSelf { .. }
| ty::AliasTermKind::InherentConstImpl { .. }
| ty::AliasTermKind::AnonConst { .. }
| ty::AliasTermKind::ProjectionConst { .. } => self.next_const_var(span).into(),
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_infer/src/infer/relate/generalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ impl<'tcx> InferCtxt<'tcx> {
| ty::AliasTermKind::OpaqueTy { .. } => {
return Err(TypeError::CyclicTy(source_term.expect_type()));
}
ty::AliasTermKind::InherentConst { .. }
ty::AliasTermKind::InherentConstSelf { .. }
| ty::AliasTermKind::InherentConstImpl { .. }
| ty::AliasTermKind::FreeConst { .. }
| ty::AliasTermKind::AnonConst { .. } => {
return Err(TypeError::CyclicConst(source_term.expect_const()));
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,15 @@ 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), self.args)
ty::AliasConst::new(
tcx,
ty::AliasConstKind::new_from_def_id(
tcx,
self.def,
ty::AliasConstInherentArgsKind::Impl,
),
self.args,
)
}
}

Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ impl<'tcx> TyCtxt<'tcx> {
}

let def_id = match ct.kind {
ty::AliasConstKind::InherentSelf { .. } => {
bug!("got AliasConstKind::InherentSelf in const_eval_resolve_for_typeck")
}
ty::AliasConstKind::Projection { def_id }
| ty::AliasConstKind::Inherent { def_id }
| ty::AliasConstKind::InherentImpl { def_id }
| ty::AliasConstKind::Free { def_id }
| ty::AliasConstKind::Anon { def_id } => def_id,
};
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,8 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
ty::ConstKind::Alias(_, alias_const) => {
let kind = match alias_const.kind {
ty::AliasConstKind::Projection { def_id }
| ty::AliasConstKind::Inherent { def_id }
| ty::AliasConstKind::InherentSelf { def_id }
| ty::AliasConstKind::InherentImpl { def_id }
| ty::AliasConstKind::Free { def_id }
| ty::AliasConstKind::Anon { def_id } => self.tcx.def_path_str(def_id),
};
Expand Down
Loading
Loading