Skip to content

Elaborate trait obligations when typechecking impls #43786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 25, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/librustc/ty/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn trait_obligations<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
-> Vec<traits::PredicateObligation<'tcx>>
{
let mut wf = WfPredicates { infcx, param_env, body_id, span, out: vec![] };
wf.compute_trait_ref(trait_ref);
wf.compute_trait_ref(trait_ref, Elaborate::All);
wf.normalize()
}

Expand All @@ -74,7 +74,7 @@ pub fn predicate_obligations<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
// (*) ok to skip binders, because wf code is prepared for it
match *predicate {
ty::Predicate::Trait(ref t) => {
wf.compute_trait_ref(&t.skip_binder().trait_ref); // (*)
wf.compute_trait_ref(&t.skip_binder().trait_ref, Elaborate::None); // (*)
}
ty::Predicate::Equate(ref t) => {
wf.compute(t.skip_binder().0);
Expand Down Expand Up @@ -114,6 +114,12 @@ struct WfPredicates<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
out: Vec<traits::PredicateObligation<'tcx>>,
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum Elaborate {
All,
None,
}

impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
fn cause(&mut self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
traits::ObligationCause::new(self.span, self.body_id, code)
Expand All @@ -135,12 +141,25 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {

/// Pushes the obligations required for `trait_ref` to be WF into
/// `self.out`.
fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elaborate) {
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
self.out.extend(obligations);

let cause = self.cause(traits::MiscObligation);
let param_env = self.param_env;

if let Elaborate::All = elaborate {
let predicates = obligations.iter()
.map(|obligation| obligation.predicate.clone())
.collect();
let implied_obligations = traits::elaborate_predicates(self.infcx.tcx, predicates);
let implied_obligations = implied_obligations.map(|pred| {
traits::Obligation::new(cause.clone(), param_env, pred)
});
self.out.extend(implied_obligations);
}

self.out.extend(obligations);

self.out.extend(
trait_ref.substs.types()
.filter(|ty| !ty.has_escaping_regions())
Expand All @@ -156,7 +175,7 @@ impl<'a, 'gcx, 'tcx> WfPredicates<'a, 'gcx, 'tcx> {
// WF and (b) the trait-ref holds. (It may also be
// normalizable and be WF that way.)
let trait_ref = data.trait_ref(self.infcx.tcx);
self.compute_trait_ref(&trait_ref);
self.compute_trait_ref(&trait_ref, Elaborate::All);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could just have Elaborate::None here, since the implied obligations are proved once and for all when typechecking the impl declaration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably .. yes ..? Should we change this then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should change this (I'll let you do that :) )


if !data.has_escaping_regions() {
let predicate = trait_ref.to_predicate();
Expand Down
25 changes: 25 additions & 0 deletions src/test/compile-fail/issue-43784-associated-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub trait Partial<X: ?Sized>: Copy {
}

pub trait Complete {
type Assoc: Partial<Self>;
}

impl<T> Partial<T> for T::Assoc where
T: Complete
{
}

impl<T> Complete for T { //~ ERROR the trait bound `T: std::marker::Copy` is not satisfied
type Assoc = T;
}
18 changes: 18 additions & 0 deletions src/test/compile-fail/issue-43784-supertrait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub trait Partial: Copy {
}

pub trait Complete: Partial {
}

impl<T> Partial for T where T: Complete {}
impl<T> Complete for T {} //~ ERROR the trait bound `T: std::marker::Copy` is not satisfied