Skip to content

Commit

Permalink
Auto merge of #70107 - lcnr:issue68977, r=eddyb
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bors committed Jun 3, 2020
2 parents 680a4b2 + 631ac9c commit ff4aff6
Show file tree
Hide file tree
Showing 33 changed files with 386 additions and 174 deletions.
2 changes: 1 addition & 1 deletion src/librustc_infer/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
self.obligations.push(Obligation::new(
self.trace.cause.clone(),
self.param_env,
ty::PredicateKind::WellFormed(b_ty).to_predicate(self.infcx.tcx),
ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx),
));
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::mir::Body;
use crate::mir::GeneratorLayout;
use crate::traits::{self, Reveal};
use crate::ty;
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef};
use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
use crate::ty::util::{Discr, IntTypeExt};
use rustc_ast::ast;
use rustc_attr as attr;
Expand Down Expand Up @@ -1061,7 +1061,7 @@ pub enum PredicateKind<'tcx> {
Projection(PolyProjectionPredicate<'tcx>),

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

/// Trait must be object-safe.
ObjectSafe(DefId),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ define_print_and_forward_display! {
ty::PredicateKind::RegionOutlives(predicate) => p!(print(predicate)),
ty::PredicateKind::TypeOutlives(predicate) => p!(print(predicate)),
ty::PredicateKind::Projection(predicate) => p!(print(predicate)),
ty::PredicateKind::WellFormed(ty) => p!(print(ty), write(" well-formed")),
ty::PredicateKind::WellFormed(arg) => p!(print(arg), write(" well-formed")),
&ty::PredicateKind::ObjectSafe(trait_def_id) => {
p!(write("the trait `"),
print_def_path(trait_def_id, &[]),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl fmt::Debug for ty::PredicateKind<'tcx> {
ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f),
ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f),
ty::PredicateKind::Projection(ref pair) => pair.fmt(f),
ty::PredicateKind::WellFormed(ty) => write!(f, "WellFormed({:?})", ty),
ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data),
ty::PredicateKind::ObjectSafe(trait_def_id) => {
write!(f, "ObjectSafe({:?})", trait_def_id)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

self.prove_predicate(
ty::PredicateKind::WellFormed(inferred_ty).to_predicate(self.tcx()),
ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()),
Locations::All(span),
ConstraintCategory::TypeAnnotation,
);
Expand Down Expand Up @@ -1268,7 +1268,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
obligations.obligations.push(traits::Obligation::new(
ObligationCause::dummy(),
param_env,
ty::PredicateKind::WellFormed(revealed_ty).to_predicate(infcx.tcx),
ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx),
));
obligations.add(
infcx
Expand Down Expand Up @@ -1612,7 +1612,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
self.check_call_dest(body, term, &sig, destination, term_location);

self.prove_predicates(
sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty)),
sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())),
term_location.to_locations(),
ConstraintCategory::Boring,
);
Expand Down
18 changes: 15 additions & 3 deletions src/librustc_trait_selection/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_hir::Node;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::fold::TypeFolder;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::{
self, fast_reject, AdtKind, SubtypePredicate, ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
TypeFoldable, WithConstness,
Expand Down Expand Up @@ -1531,13 +1532,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
err
}

ty::PredicateKind::WellFormed(ty) => {
ty::PredicateKind::WellFormed(arg) => {
// Same hacky approach as above to avoid deluging user
// with error messages.
if ty.references_error() || self.tcx.sess.has_errors() {
if arg.references_error() || self.tcx.sess.has_errors() {
return;
}
self.need_type_info_err(body_id, span, ty, ErrorCode::E0282)

match arg.unpack() {
GenericArgKind::Lifetime(lt) => {
span_bug!(span, "unexpected well formed predicate: {:?}", lt)
}
GenericArgKind::Type(ty) => {
self.need_type_info_err(body_id, span, ty, ErrorCode::E0282)
}
GenericArgKind::Const(ct) => {
self.need_type_info_err_const(body_id, span, ct, ErrorCode::E0282)
}
}
}

ty::PredicateKind::Subtype(ref data) => {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_trait_selection/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,17 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
}
}

&ty::PredicateKind::WellFormed(ty) => {
&ty::PredicateKind::WellFormed(arg) => {
match wf::obligations(
self.selcx.infcx(),
obligation.param_env,
obligation.cause.body_id,
ty,
arg,
obligation.cause.span,
) {
None => {
pending_obligation.stalled_on =
vec![TyOrConstInferVar::maybe_from_ty(ty).unwrap()];
vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()];
ProcessResult::Unchanged
}
Some(os) => ProcessResult::Changed(mk_pending(os)),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trait_selection/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

&ty::PredicateKind::WellFormed(ty) => match wf::obligations(
&ty::PredicateKind::WellFormed(arg) => match wf::obligations(
self.infcx,
obligation.param_env,
obligation.cause.body_id,
ty,
arg,
obligation.cause.span,
) {
Some(mut obligations) => {
Expand Down
Loading

0 comments on commit ff4aff6

Please sign in to comment.