Skip to content

Commit ff4aff6

Browse files
committed
Auto merge of #70107 - lcnr:issue68977, r=eddyb
WF-check all ty::Const's, not just array lengths. fixes #68977 This PR removes the special case for array length in `wf::compute` and checks the well formedness of all consts. Changes `PredicateKind::WellFormed` to take a `GenericArg` and updates `wf::obligations`.
2 parents 680a4b2 + 631ac9c commit ff4aff6

File tree

33 files changed

+386
-174
lines changed

33 files changed

+386
-174
lines changed

src/librustc_infer/infer/combine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
307307
self.obligations.push(Obligation::new(
308308
self.trace.cause.clone(),
309309
self.param_env,
310-
ty::PredicateKind::WellFormed(b_ty).to_predicate(self.infcx.tcx),
310+
ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx),
311311
));
312312
}
313313

src/librustc_middle/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::mir::Body;
1414
use crate::mir::GeneratorLayout;
1515
use crate::traits::{self, Reveal};
1616
use crate::ty;
17-
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef};
17+
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
1818
use crate::ty::util::{Discr, IntTypeExt};
1919
use rustc_ast::ast;
2020
use rustc_attr as attr;
@@ -1061,7 +1061,7 @@ pub enum PredicateKind<'tcx> {
10611061
Projection(PolyProjectionPredicate<'tcx>),
10621062

10631063
/// No syntax: `T` well-formed.
1064-
WellFormed(Ty<'tcx>),
1064+
WellFormed(GenericArg<'tcx>),
10651065

10661066
/// Trait must be object-safe.
10671067
ObjectSafe(DefId),

src/librustc_middle/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ define_print_and_forward_display! {
20312031
ty::PredicateKind::RegionOutlives(predicate) => p!(print(predicate)),
20322032
ty::PredicateKind::TypeOutlives(predicate) => p!(print(predicate)),
20332033
ty::PredicateKind::Projection(predicate) => p!(print(predicate)),
2034-
ty::PredicateKind::WellFormed(ty) => p!(print(ty), write(" well-formed")),
2034+
ty::PredicateKind::WellFormed(arg) => p!(print(arg), write(" well-formed")),
20352035
&ty::PredicateKind::ObjectSafe(trait_def_id) => {
20362036
p!(write("the trait `"),
20372037
print_def_path(trait_def_id, &[]),

src/librustc_middle/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl fmt::Debug for ty::PredicateKind<'tcx> {
236236
ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
237237
ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
238238
ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
239-
ty::PredicateKind::WellFormed(ty) => write!(f, "WellFormed({:?})", ty),
239+
ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
240240
ty::PredicateKind::ObjectSafe(trait_def_id) => {
241241
write!(f, "ObjectSafe({:?})", trait_def_id)
242242
}

src/librustc_mir/borrow_check/type_check/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10161016
}
10171017

10181018
self.prove_predicate(
1019-
ty::PredicateKind::WellFormed(inferred_ty).to_predicate(self.tcx()),
1019+
ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()),
10201020
Locations::All(span),
10211021
ConstraintCategory::TypeAnnotation,
10221022
);
@@ -1268,7 +1268,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
12681268
obligations.obligations.push(traits::Obligation::new(
12691269
ObligationCause::dummy(),
12701270
param_env,
1271-
ty::PredicateKind::WellFormed(revealed_ty).to_predicate(infcx.tcx),
1271+
ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx),
12721272
));
12731273
obligations.add(
12741274
infcx
@@ -1612,7 +1612,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16121612
self.check_call_dest(body, term, &sig, destination, term_location);
16131613

16141614
self.prove_predicates(
1615-
sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty)),
1615+
sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())),
16161616
term_location.to_locations(),
16171617
ConstraintCategory::Boring,
16181618
);

src/librustc_trait_selection/traits/error_reporting/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_hir::Node;
1919
use rustc_middle::mir::interpret::ErrorHandled;
2020
use rustc_middle::ty::error::ExpectedFound;
2121
use rustc_middle::ty::fold::TypeFolder;
22+
use rustc_middle::ty::subst::GenericArgKind;
2223
use rustc_middle::ty::{
2324
self, fast_reject, AdtKind, SubtypePredicate, ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
2425
TypeFoldable, WithConstness,
@@ -1531,13 +1532,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15311532
err
15321533
}
15331534

1534-
ty::PredicateKind::WellFormed(ty) => {
1535+
ty::PredicateKind::WellFormed(arg) => {
15351536
// Same hacky approach as above to avoid deluging user
15361537
// with error messages.
1537-
if ty.references_error() || self.tcx.sess.has_errors() {
1538+
if arg.references_error() || self.tcx.sess.has_errors() {
15381539
return;
15391540
}
1540-
self.need_type_info_err(body_id, span, ty, ErrorCode::E0282)
1541+
1542+
match arg.unpack() {
1543+
GenericArgKind::Lifetime(lt) => {
1544+
span_bug!(span, "unexpected well formed predicate: {:?}", lt)
1545+
}
1546+
GenericArgKind::Type(ty) => {
1547+
self.need_type_info_err(body_id, span, ty, ErrorCode::E0282)
1548+
}
1549+
GenericArgKind::Const(ct) => {
1550+
self.need_type_info_err_const(body_id, span, ct, ErrorCode::E0282)
1551+
}
1552+
}
15411553
}
15421554

15431555
ty::PredicateKind::Subtype(ref data) => {

src/librustc_trait_selection/traits/fulfill.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -459,17 +459,17 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
459459
}
460460
}
461461

462-
&ty::PredicateKind::WellFormed(ty) => {
462+
&ty::PredicateKind::WellFormed(arg) => {
463463
match wf::obligations(
464464
self.selcx.infcx(),
465465
obligation.param_env,
466466
obligation.cause.body_id,
467-
ty,
467+
arg,
468468
obligation.cause.span,
469469
) {
470470
None => {
471471
pending_obligation.stalled_on =
472-
vec![TyOrConstInferVar::maybe_from_ty(ty).unwrap()];
472+
vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
473473
ProcessResult::Unchanged
474474
}
475475
Some(os) => ProcessResult::Changed(mk_pending(os)),

src/librustc_trait_selection/traits/select.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
436436
}
437437
}
438438

439-
&ty::PredicateKind::WellFormed(ty) => match wf::obligations(
439+
&ty::PredicateKind::WellFormed(arg) => match wf::obligations(
440440
self.infcx,
441441
obligation.param_env,
442442
obligation.cause.body_id,
443-
ty,
443+
arg,
444444
obligation.cause.span,
445445
) {
446446
Some(mut obligations) => {

0 commit comments

Comments
 (0)