Skip to content

stop returning non-principal objects in issue #33140 relate mode #57335

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

Closed
wants to merge 2 commits 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
55 changes: 27 additions & 28 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use rustc_target::spec::abi;
use hir as ast;
use traits;

use rustc_data_structures::fx::FxHashSet;

pub type RelateResult<'tcx, T> = Result<T, TypeError<'tcx>>;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -601,40 +603,37 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>> {
use ty::ExistentialPredicate::*;

let tcx = relation.tcx();
let (a_buf, b_buf);
let (a_norm, b_norm): (&[_], &[_]) = match relation.trait_object_mode() {
TraitObjectMode::NoSquash => {
(a, b)
}
TraitObjectMode::SquashAutoTraitsIssue33140 => {
// Treat auto-trait "principal" components as equal
// to the non-principal components, to make
// `dyn Send+Sync = dyn Sync+Send`.
let normalize = |d: &[ty::ExistentialPredicate<'tcx>]| {
let mut result: Vec<_> = d.iter().map(|pi| match pi {
Trait(ref a) if tcx.trait_is_auto(a.def_id) => {
AutoTrait(a.def_id)
},
other => *other
}).collect();

result.sort_by(|a, b| a.stable_cmp(tcx, b));
result.dedup();
result
};

a_buf = normalize(a);
b_buf = normalize(b);

(&a_buf, &b_buf)
if let TraitObjectMode::SquashAutoTraitsIssue33140 = relation.trait_object_mode() {
// Treat auto-trait "principal" components as equal
// to the non-principal components, to make
// `dyn Send+Sync = dyn Sync+Send`.
//
// In that case,both types will be "fully resolved" (because
// auto-traits can't have type parameters or lifetimes), and we
// can just return either of them - we don't perform a full
// relation because that would "spread" the unnormalized types.

let auto_traits = |d: &[ty::ExistentialPredicate<'tcx>]| {
d.iter().map(|pi| match pi {
Trait(ref a) if tcx.trait_is_auto(a.def_id) => {
Ok(a.def_id)
},
AutoTrait(def_id) => Ok(*def_id),
_ => Err(()),
}).collect::<Result<FxHashSet<_>, ()>>()
};

match (&auto_traits(a), &auto_traits(b)) {
(Ok(a_dids), Ok(b_dids)) if a_dids == b_dids => return Ok(a),
_ => {}
}
};

if a_norm.len() != b_norm.len() {
if a.len() != b.len() {
return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
}

let v = a_norm.iter().zip(b_norm.iter()).map(|(ep_a, ep_b)| {
let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
use ty::ExistentialPredicate::*;
match (*ep_a, *ep_b) {
(Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,7 @@ fn predicates_defined_on<'a, 'tcx>(
.predicates
.extend(inferred_outlives.iter().map(|&p| (p, span)));
}
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
result
}

Expand Down Expand Up @@ -1645,6 +1646,7 @@ fn predicates_of<'a, 'tcx>(
.predicates
.push((ty::TraitRef::identity(tcx, def_id).to_predicate(), span));
}
debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
result
}

Expand Down Expand Up @@ -1972,10 +1974,12 @@ fn explicit_predicates_of<'a, 'tcx>(
);
}

Lrc::new(ty::GenericPredicates {
let result = Lrc::new(ty::GenericPredicates {
parent: generics.parent,
predicates,
})
});
debug!("explicit_predicates_of(def_id={:?}) = {:?}", def_id, result);
result
}

pub enum SizedByDefault {
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/issues/issue-57162.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compile-pass

trait Foo {}
impl Foo for dyn Send {}

impl<T: Sync + Sync> Foo for T {}
fn main() {}