Skip to content

Commit 3d29ce7

Browse files
authored
Rollup merge of #114314 - compiler-errors:sized-crit, r=lcnr
Tweaks to `adt_sized_constraint` fixes a comment, but also some other nits. r? lcnr
2 parents f338a1f + ac6f2f0 commit 3d29ce7

File tree

7 files changed

+27
-25
lines changed

7 files changed

+27
-25
lines changed

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14621462
let traits: Vec<_> =
14631463
self.probe_traits_that_match_assoc_ty(qself_ty, assoc_ident);
14641464

1465-
// Don't print `TyErr` to the user.
1465+
// Don't print `ty::Error` to the user.
14661466
self.report_ambiguous_associated_type(
14671467
span,
14681468
&[qself_ty.to_string()],

Diff for: compiler/rustc_hir_typeck/src/closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
795795

796796
/// Converts the types that the user supplied, in case that doing
797797
/// so should yield an error, but returns back a signature where
798-
/// all parameters are of type `TyErr`.
798+
/// all parameters are of type `ty::Error`.
799799
fn error_sig_of_closure(
800800
&self,
801801
decl: &hir::FnDecl<'_>,

Diff for: compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ rustc_queries! {
706706
separate_provide_extern
707707
}
708708

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

Diff for: compiler/rustc_middle/src/ty/adt.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,10 @@ impl<'tcx> AdtDef<'tcx> {
562562
tcx.adt_destructor(self.did())
563563
}
564564

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

Diff for: compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub(in crate::solve) fn instantiate_constituent_tys_for_sized_trait<'tcx>(
150150

151151
ty::Adt(def, args) => {
152152
let sized_crit = def.sized_constraint(ecx.tcx());
153-
Ok(sized_crit.iter_instantiated_copied(ecx.tcx(), args).collect())
153+
Ok(sized_crit.iter_instantiated(ecx.tcx(), args).collect())
154154
}
155155
}
156156
}

Diff for: compiler/rustc_trait_selection/src/traits/select/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20992099
Where(
21002100
obligation
21012101
.predicate
2102-
.rebind(sized_crit.iter_instantiated_copied(self.tcx(), args).collect()),
2102+
.rebind(sized_crit.iter_instantiated(self.tcx(), args).collect()),
21032103
)
21042104
}
21052105

Diff for: compiler/rustc_ty_utils/src/ty.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn sized_constraint_for_ty<'tcx>(
4242
let adt_tys = adt.sized_constraint(tcx);
4343
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
4444
adt_tys
45-
.iter_instantiated_copied(tcx, args)
45+
.iter_instantiated(tcx, args)
4646
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
4747
.collect()
4848
}
@@ -58,11 +58,18 @@ fn sized_constraint_for_ty<'tcx>(
5858
// we know that `T` is Sized and do not need to check
5959
// it on the impl.
6060

61-
let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
62-
let sized_predicate =
63-
ty::TraitRef::new(tcx, sized_trait, [ty]).without_const().to_predicate(tcx);
61+
let Some(sized_trait_def_id) = tcx.lang_items().sized_trait() else { return vec![ty] };
6462
let predicates = tcx.predicates_of(adtdef.did()).predicates;
65-
if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
63+
if predicates.iter().any(|(p, _)| {
64+
p.as_trait_clause().is_some_and(|trait_pred| {
65+
trait_pred.def_id() == sized_trait_def_id
66+
&& trait_pred.self_ty().skip_binder() == ty
67+
})
68+
}) {
69+
vec![]
70+
} else {
71+
vec![ty]
72+
}
6673
}
6774

6875
Placeholder(..) | Bound(..) | Infer(..) => {
@@ -92,10 +99,13 @@ fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
9299
/// - a tuple of type parameters or projections, if there are multiple
93100
/// such.
94101
/// - an Error, if a type is infinitely sized
95-
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
102+
fn adt_sized_constraint<'tcx>(
103+
tcx: TyCtxt<'tcx>,
104+
def_id: DefId,
105+
) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
96106
if let Some(def_id) = def_id.as_local() {
97107
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
98-
return tcx.mk_type_list(&[Ty::new_misc_error(tcx)]);
108+
return ty::EarlyBinder::bind(tcx.mk_type_list(&[Ty::new_misc_error(tcx)]));
99109
}
100110
}
101111
let def = tcx.adt_def(def_id);
@@ -107,7 +117,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
107117

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

110-
result
120+
ty::EarlyBinder::bind(result)
111121
}
112122

113123
/// See `ParamEnv` struct definition for details.

0 commit comments

Comments
 (0)