Skip to content
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

chalkify: Fix lowering of traits with supertraits #49971

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions src/librustc_traits/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,19 @@ fn program_clauses_for_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefI
// ```

// `FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn>)`, for each where clause WC
// FIXME: Remove the [1..] slice; this is a hack because the query
// FIXME: Remove the filter; this is a hack because the query
// predicates_of currently includes the trait itself (`Self: Trait<P1..Pn>`).
let where_clauses = &tcx.predicates_of(def_id).predicates;
let implied_bound_clauses =
where_clauses[1..].into_iter()
where_clauses.into_iter()
.filter(|wc| {
if let ty::Predicate::Trait(pred) = wc {
if pred.skip_binder().def_id() == def_id {
return false;
}
}
true
})
Copy link
Member

Choose a reason for hiding this comment

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

It seems like this would also skip a predicate of the form:

trait A where <Self as A>::Item: A {
    type Item;
}

or equivalently:

trait A {
    type Item: A;
}

since you are only relying on the def id. Actually maybe it would be better to just wait for your PR which removes the Self: Trait predicate...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good catch. I'll try and make progress on that PR today. A patch that might possibly still work here is to filter out only the first predicate that matches this condition, but it still feels quite brittle!

.map(|wc| implied_bound_from_trait(tcx, trait_pred, wc));

Lrc::new(tcx.mk_clauses(clauses.chain(implied_bound_clauses)))
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/chalkify/lower_trait_supertrait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2018 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.

#![feature(rustc_attrs)]

trait Bar {}

#[rustc_dump_program_clauses] //~ ERROR Implemented
//~^ ERROR FromEnv
trait Foo: Bar {}

fn main() {
println!("hello");
}
14 changes: 14 additions & 0 deletions src/test/ui/chalkify/lower_trait_supertrait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: Implemented(Self: Foo) :- FromEnv(Self: Foo).
--> $DIR/lower_trait_supertrait.rs:15:1
|
LL | #[rustc_dump_program_clauses] //~ ERROR Implemented
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: FromEnv(Self: Bar) :- FromEnv(Self: Foo).
--> $DIR/lower_trait_supertrait.rs:15:1
|
LL | #[rustc_dump_program_clauses] //~ ERROR Implemented
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors