Skip to content

Mutable references should not be Copy/Clone #447

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 5 commits into from
May 9, 2020
Merged
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
3 changes: 2 additions & 1 deletion book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Some common examples of auto traits are `Send` and `Sync`.
| trait objects | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ |
| functions ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| raw ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| references | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| immutable refs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| mutable refs | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| slices | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| arrays❌ | ❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| closures❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
Expand Down
6 changes: 4 additions & 2 deletions chalk-solve/src/clauses/builtin_traits/copy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::clauses::builtin_traits::needs_impl_for_tys;
use crate::clauses::ClauseBuilder;
use crate::{Interner, RustIrDatabase, TraitRef};
use chalk_ir::{ApplicationTy, Substitution, TyData, TypeName};
use chalk_ir::{ApplicationTy, Mutability, Substitution, TyData, TypeName};

fn push_tuple_copy_conditions<I: Interner>(
db: &dyn RustIrDatabase<I>,
Expand Down Expand Up @@ -41,7 +41,9 @@ pub fn add_copy_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_copy_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Raw(_) | TypeName::Ref(_) => builder.push_fact(trait_ref.clone()),
TypeName::Raw(_) | TypeName::Ref(Mutability::Not) => {
builder.push_fact(trait_ref.clone())
}
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
2 changes: 1 addition & 1 deletion tests/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ fn slices() {
}

error_msg {
"parse error: UnrecognizedToken { token: (29, Token(30, \"]\"), 30), expected: [\"\\\"&\\\"\", \"\\\"(\\\"\", \"\\\"*\\\"\", \"\\\"<\\\"\", \"\\\"[\\\"\", \"\\\"bool\\\"\", \"\\\"char\\\"\", \"\\\"dyn\\\"\", \"\\\"f32\\\"\", \"\\\"f64\\\"\", \"\\\"fn\\\"\", \"\\\"for\\\"\", \"\\\"i128\\\"\", \"\\\"i16\\\"\", \"\\\"i32\\\"\", \"\\\"i64\\\"\", \"\\\"i8\\\"\", \"\\\"isize\\\"\", \"\\\"u128\\\"\", \"\\\"u16\\\"\", \"\\\"u32\\\"\", \"\\\"u64\\\"\", \"\\\"u8\\\"\", \"\\\"usize\\\"\", \"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
"parse error: UnrecognizedToken { token: (29, Token(31, \"]\"), 30), expected: [\"\\\"&\\\"\", \"\\\"(\\\"\", \"\\\"*\\\"\", \"\\\"<\\\"\", \"\\\"[\\\"\", \"\\\"bool\\\"\", \"\\\"char\\\"\", \"\\\"dyn\\\"\", \"\\\"f32\\\"\", \"\\\"f64\\\"\", \"\\\"fn\\\"\", \"\\\"for\\\"\", \"\\\"i128\\\"\", \"\\\"i16\\\"\", \"\\\"i32\\\"\", \"\\\"i64\\\"\", \"\\\"i8\\\"\", \"\\\"isize\\\"\", \"\\\"u128\\\"\", \"\\\"u16\\\"\", \"\\\"u32\\\"\", \"\\\"u64\\\"\", \"\\\"u8\\\"\", \"\\\"usize\\\"\", \"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}
}
69 changes: 65 additions & 4 deletions tests/test/refs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[test]
fn refs_are_well_formed() {
fn immut_refs_are_well_formed() {
test! {
program { }

Expand All @@ -14,7 +14,7 @@ fn refs_are_well_formed() {
}

#[test]
fn refs_are_sized() {
fn immut_refs_are_sized() {
test! {
program {
#[lang(sized)]
Expand All @@ -30,7 +30,36 @@ fn refs_are_sized() {
}

#[test]
fn refs_are_copy() {
fn mut_refs_are_well_formed() {
test! {
program { }

goal {
forall<'a, T> { WellFormed(&'a mut T) }
} yields {
"Unique; substitution [], lifetime constraints []"
}
}
}

#[test]
fn mut_refs_are_sized() {
test! {
program {
#[lang(sized)]
trait Sized { }
}

goal {
forall<'a, T> { &'a mut T: Sized }
} yields {
"Unique; substitution [], lifetime constraints []"
}
}
}

#[test]
fn immut_refs_are_copy() {
test! {
program {
#[lang(copy)]
Expand All @@ -46,7 +75,7 @@ fn refs_are_copy() {
}

#[test]
fn refs_are_clone() {
fn immut_refs_are_clone() {
test! {
program {
#[lang(clone)]
Expand All @@ -60,3 +89,35 @@ fn refs_are_clone() {
}
}
}

#[test]
fn mut_refs_are_not_copy() {
test! {
program {
#[lang(copy)]
trait Copy { }
}

goal {
forall<'a, T> { not { &'a mut T: Copy } }
} yields {
"Unique; substitution [], lifetime constraints []"
}
}
}

#[test]
fn mut_refs_are_not_clone() {
test! {
program {
#[lang(clone)]
trait Clone { }
}

goal {
forall<'a, T> { not { &'a mut T: Clone } }
} yields {
"Unique; substitution [], lifetime constraints []"
}
}
}