Restore tuple unsizing feature gate

This commit is contained in:
Michael Goulet 2023-07-16 22:42:46 +00:00
parent 24eefd08e2
commit c02d1a6553
7 changed files with 57 additions and 19 deletions

View File

@ -622,6 +622,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target])
)];
let mut has_unsized_tuple_coercion = false;
let mut has_trait_upcasting_coercion = None;
// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
@ -686,11 +687,16 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
Ok(Some(impl_source)) => {
if matches!(impl_source, traits::ImplSource::TraitUpcasting(..)) {
has_trait_upcasting_coercion =
Some((trait_pred.self_ty(), trait_pred.trait_ref.args.type_at(1)));
match impl_source {
traits::ImplSource::TraitUpcasting(..) => {
has_trait_upcasting_coercion =
Some((trait_pred.self_ty(), trait_pred.trait_ref.args.type_at(1)));
}
traits::ImplSource::TupleUnsizing(_) => {
has_unsized_tuple_coercion = true;
}
_ => {}
}
queue.extend(impl_source.nested_obligations())
}
}
@ -711,6 +717,16 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
err.emit();
}
if has_unsized_tuple_coercion && !self.tcx.features().unsized_tuple_coercion {
feature_err(
&self.tcx.sess.parse_sess,
sym::unsized_tuple_coercion,
self.cause.span,
"unsized tuple coercion is not stable enough for use and is subject to change",
)
.emit();
}
Ok(coercion)
}

View File

@ -657,6 +657,9 @@ pub enum ImplSource<'tcx, N> {
/// Successful resolution for a builtin trait.
Builtin(Vec<N>),
// Unsizing a tuple like `(A, B, ..., X)` to `(A, B, ..., Y)` if `X` unsizes to `Y`
TupleUnsizing(Vec<N>),
/// ImplSource for trait upcasting coercion
TraitUpcasting(ImplSourceTraitUpcastingData<N>),
}
@ -665,7 +668,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
pub fn nested_obligations(self) -> Vec<N> {
match self {
ImplSource::UserDefined(i) => i.nested,
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => n,
ImplSource::Object(d) => d.nested,
ImplSource::TraitUpcasting(d) => d.nested,
}
@ -674,7 +677,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
pub fn borrow_nested_obligations(&self) -> &[N] {
match self {
ImplSource::UserDefined(i) => &i.nested,
ImplSource::Param(n, _) | ImplSource::Builtin(n) => &n,
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => &n,
ImplSource::Object(d) => &d.nested,
ImplSource::TraitUpcasting(d) => &d.nested,
}
@ -683,7 +686,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] {
match self {
ImplSource::UserDefined(i) => &mut i.nested,
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
ImplSource::Param(n, _) | ImplSource::Builtin(n) | ImplSource::TupleUnsizing(n) => n,
ImplSource::Object(d) => &mut d.nested,
ImplSource::TraitUpcasting(d) => &mut d.nested,
}
@ -701,6 +704,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
}),
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
ImplSource::Builtin(n) => ImplSource::Builtin(n.into_iter().map(f).collect()),
ImplSource::TupleUnsizing(n) => {
ImplSource::TupleUnsizing(n.into_iter().map(f).collect())
}
ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData {
vtable_base: o.vtable_base,
nested: o.nested.into_iter().map(f).collect(),

View File

@ -17,6 +17,8 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
write!(f, "ImplSourceParamData({:?}, {:?})", n, ct)
}
super::ImplSource::TupleUnsizing(ref d) => write!(f, "{:?}", d),
super::ImplSource::TraitUpcasting(ref d) => write!(f, "{:?}", d),
}
}

View File

@ -456,6 +456,9 @@ fn rematch_unsize<'tcx>(
goal.param_env,
ty::TraitRef::new(tcx, goal.predicate.def_id(), [*a_last_ty, *b_last_ty]),
));
// We need to be able to detect tuple unsizing to require its feature gate.
return Ok(Some(ImplSource::TupleUnsizing(nested)));
}
// FIXME: We *could* ICE here if either:
// 1. the certainty is `Certainty::Yes`,

View File

@ -1925,7 +1925,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
// why we special case object types.
false
}
| super::ImplSource::TraitUpcasting(_) => {
super::ImplSource::TraitUpcasting(_)
| super::ImplSource::TupleUnsizing(_) => {
// These traits have no associated types.
selcx.tcx().sess.delay_span_bug(
obligation.cause.span,
@ -2005,7 +2006,8 @@ fn confirm_select_candidate<'cx, 'tcx>(
}
super::ImplSource::Object(_)
| super::ImplSource::Param(..)
| super::ImplSource::TraitUpcasting(_) => {
| super::ImplSource::TraitUpcasting(_)
| super::ImplSource::TupleUnsizing(_) => {
// we don't create Select candidates with this kind of resolution
span_bug!(
obligation.cause.span,

View File

@ -114,8 +114,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
BuiltinUnsizeCandidate => {
let data = self.confirm_builtin_unsize_candidate(obligation)?;
ImplSource::Builtin(data)
let source =
self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
let target = obligation.predicate.skip_binder().trait_ref.args.type_at(1);
let target = self.infcx.shallow_resolve(target);
let data = self.confirm_builtin_unsize_candidate(obligation, source, target)?;
// If the source and target are both unsize goals, then we need to signify that
// this is tuple unsizing so that during unsized coercion we require the proper
// feature gate.
if matches!(source.kind(), ty::Tuple(..)) && matches!(target.kind(), ty::Tuple(..))
{
ImplSource::TupleUnsizing(data)
} else {
ImplSource::Builtin(data)
}
}
TraitUpcastingUnsizeCandidate(idx) => {
@ -1000,15 +1012,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
fn confirm_builtin_unsize_candidate(
&mut self,
obligation: &PolyTraitObligation<'tcx>,
source: Ty<'tcx>,
target: Ty<'tcx>,
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
let tcx = self.tcx();
// `assemble_candidates_for_unsizing` should ensure there are no late-bound
// regions here. See the comment there for more details.
let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
let target = obligation.predicate.skip_binder().trait_ref.args.type_at(1);
let target = self.infcx.shallow_resolve(target);
debug!(?source, ?target, "confirm_builtin_unsize_candidate");
let mut nested = vec![];

View File

@ -290,7 +290,9 @@ fn resolve_associated_item<'tcx>(
None
}
}
traits::ImplSource::Param(..) | traits::ImplSource::TraitUpcasting(_) => None,
traits::ImplSource::Param(..)
| traits::ImplSource::TraitUpcasting(_)
| traits::ImplSource::TupleUnsizing(_) => None,
})
}