Skip to content

Commit

Permalink
Merge pull request #503 from Areredify/const_wf_fix
Browse files Browse the repository at this point in the history
complete wf checks for Copy impls
  • Loading branch information
jackh726 authored Jun 10, 2020
2 parents bf33634 + e62d26e commit a6400eb
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 12 deletions.
19 changes: 12 additions & 7 deletions chalk-solve/src/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,19 @@ impl WfWellKnownGoals {
let ty = trait_ref.self_type_parameter(interner);
let ty_data = ty.data(interner);

// Implementations for scalars, pointer types and never type are provided by libcore.
// User implementations on types other than ADTs are forbidden.
let (adt_id, substitution) = match ty_data {
TyData::Apply(ApplicationTy {
name: TypeName::Adt(adt_id),
substitution,
}) => (*adt_id, substitution),
// TODO(areredify)
// when #368 lands, extend this to handle everything accordingly
_ => return None,
TyData::Apply(ApplicationTy { name, substitution }) => match name {
TypeName::Scalar(_)
| TypeName::Raw(_)
| TypeName::Ref(Mutability::Not)
| TypeName::Never => return None,
TypeName::Adt(adt_id) => (*adt_id, substitution),
_ => return Some(GoalData::CannotProve(()).intern(interner)),
},

_ => return Some(GoalData::CannotProve(()).intern(interner)),
};

// not { Implemented(ImplSelfTy: Drop) }
Expand Down
125 changes: 120 additions & 5 deletions tests/test/wf_lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,19 +712,17 @@ fn struct_sized_constraints() {

#[test]
fn copy_constraints() {
lowering_error! {
lowering_success! {
program {
#[lang(copy)]
trait Copy { }

#[lang(drop)]
trait Drop { }

struct S<T> { t: T }
struct S<T1, T2> { t1: T1, t2: T2 }

impl<T> Copy for S<T> { }
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
impl<T1, T2> Copy for S<T1, T2> where T1: Copy, T2: Copy { }
}
}

Expand All @@ -744,6 +742,34 @@ fn copy_constraints() {
}
}

// Copy implementations for a struct with non-copy field
lowering_error! {
program {
#[lang(copy)]
trait Copy { }

struct S<T> { t: T }

impl<T> Copy for S<T> { }
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

struct S<T1, T2> { t1: T1, t2: T2 }

impl<T1, T2> Copy for S<T1, T2> where T2: Copy { }
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

// Copy implemenation for a Drop type
lowering_error! {
program {
#[lang(copy)]
Expand All @@ -761,6 +787,95 @@ fn copy_constraints() {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

// Tests for Copy impls for builtin types
lowering_success! {
program {
#[lang(copy)]
trait Copy { }

#[lang(drop)]
trait Drop { }

impl Copy for u8 {}
impl Copy for f32 {}
impl Copy for char {}
impl Copy for bool {}
impl<T> Copy for *const T {}
impl<T> Copy for *mut T {}
impl<'a, T> Copy for &'a T {}
impl Copy for ! {}
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

impl<'a, T> Copy for &'a mut T {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

#[object_safe]
trait Trait {}

impl<'a> Copy for dyn Trait + 'a {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

impl Copy for fn(u32) {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

impl Copy for str {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

impl Copy for [u32; 4] {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}

lowering_error! {
program {
#[lang(copy)]
trait Copy { }

impl Copy for [u32] {}
} error_msg {
"trait impl for `Copy` does not meet well-formedness requirements"
}
}
}

#[test]
Expand Down

0 comments on commit a6400eb

Please sign in to comment.