Skip to content

Commit

Permalink
Rollup merge of #114314 - compiler-errors:sized-crit, r=lcnr
Browse files Browse the repository at this point in the history
Tweaks to `adt_sized_constraint`

fixes a comment, but also some other nits.

r? lcnr
  • Loading branch information
matthiaskrgr authored Aug 2, 2023
2 parents f338a1f + ac6f2f0 commit 3d29ce7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let traits: Vec<_> =
self.probe_traits_that_match_assoc_ty(qself_ty, assoc_ident);

// Don't print `TyErr` to the user.
// Don't print `ty::Error` to the user.
self.report_ambiguous_associated_type(
span,
&[qself_ty.to_string()],
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

/// Converts the types that the user supplied, in case that doing
/// so should yield an error, but returns back a signature where
/// all parameters are of type `TyErr`.
/// all parameters are of type `ty::Error`.
fn error_sig_of_closure(
&self,
decl: &hir::FnDecl<'_>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ rustc_queries! {
separate_provide_extern
}

query adt_sized_constraint(key: DefId) -> &'tcx [Ty<'tcx>] {
query adt_sized_constraint(key: DefId) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
desc { |tcx| "computing `Sized` constraints for `{}`", tcx.def_path_str(key) }
}

Expand Down
16 changes: 4 additions & 12 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,10 @@ impl<'tcx> AdtDef<'tcx> {
tcx.adt_destructor(self.did())
}

/// Returns a list of types such that `Self: Sized` if and only
/// if that type is `Sized`, or `TyErr` if this type is recursive.
///
/// Oddly enough, checking that the sized-constraint is `Sized` is
/// actually more expressive than checking all members:
/// the `Sized` trait is inductive, so an associated type that references
/// `Self` would prevent its containing ADT from being `Sized`.
///
/// Due to normalization being eager, this applies even if
/// the associated type is behind a pointer (e.g., issue #31299).
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> {
ty::EarlyBinder::bind(tcx.adt_sized_constraint(self.did()))
/// Returns a list of types such that `Self: Sized` if and only if that
/// type is `Sized`, or `ty::Error` if this type has a recursive layout.
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
tcx.adt_sized_constraint(self.did())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_sized_trait<'tcx>(

ty::Adt(def, args) => {
let sized_crit = def.sized_constraint(ecx.tcx());
Ok(sized_crit.iter_instantiated_copied(ecx.tcx(), args).collect())
Ok(sized_crit.iter_instantiated(ecx.tcx(), args).collect())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
Where(
obligation
.predicate
.rebind(sized_crit.iter_instantiated_copied(self.tcx(), args).collect()),
.rebind(sized_crit.iter_instantiated(self.tcx(), args).collect()),
)
}

Expand Down
26 changes: 18 additions & 8 deletions compiler/rustc_ty_utils/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn sized_constraint_for_ty<'tcx>(
let adt_tys = adt.sized_constraint(tcx);
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
adt_tys
.iter_instantiated_copied(tcx, args)
.iter_instantiated(tcx, args)
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
.collect()
}
Expand All @@ -58,11 +58,18 @@ fn sized_constraint_for_ty<'tcx>(
// we know that `T` is Sized and do not need to check
// it on the impl.

let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
let sized_predicate =
ty::TraitRef::new(tcx, sized_trait, [ty]).without_const().to_predicate(tcx);
let Some(sized_trait_def_id) = tcx.lang_items().sized_trait() else { return vec![ty] };
let predicates = tcx.predicates_of(adtdef.did()).predicates;
if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
if predicates.iter().any(|(p, _)| {
p.as_trait_clause().is_some_and(|trait_pred| {
trait_pred.def_id() == sized_trait_def_id
&& trait_pred.self_ty().skip_binder() == ty
})
}) {
vec![]
} else {
vec![ty]
}
}

Placeholder(..) | Bound(..) | Infer(..) => {
Expand Down Expand Up @@ -92,10 +99,13 @@ fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
/// - a tuple of type parameters or projections, if there are multiple
/// such.
/// - an Error, if a type is infinitely sized
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
fn adt_sized_constraint<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
if let Some(def_id) = def_id.as_local() {
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
return tcx.mk_type_list(&[Ty::new_misc_error(tcx)]);
return ty::EarlyBinder::bind(tcx.mk_type_list(&[Ty::new_misc_error(tcx)]));
}
}
let def = tcx.adt_def(def_id);
Expand All @@ -107,7 +117,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {

debug!("adt_sized_constraint: {:?} => {:?}", def, result);

result
ty::EarlyBinder::bind(result)
}

/// See `ParamEnv` struct definition for details.
Expand Down

0 comments on commit 3d29ce7

Please sign in to comment.