Skip to content

Add never type to TypeName #466

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 22, 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
5 changes: 3 additions & 2 deletions book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ Some common examples of auto traits are `Send` and `Sync`.
| structs | ⚬ | ⚬ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ✅ |
| scalar types | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| str | 📚 | 📚 | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| never type | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| trait objects | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ |
| functions ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| raw ptrs | | | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| immutable refs | | | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| raw ptrs | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| immutable refs | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| mutable refs | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| slices | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| arrays❌ | ❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
Expand Down
6 changes: 6 additions & 0 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,12 @@ impl LowerTy for Ty {
substitution: chalk_ir::Substitution::empty(interner),
})
.intern(interner)),

Ty::Never => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Never,
substitution: chalk_ir::Substitution::empty(interner),
})
.intern(interner)),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl<I: Interner> Debug for TypeName<I> {
TypeName::FnDef(fn_def) => write!(fmt, "{:?}", fn_def),
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Ref(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Never => write!(fmt, "Never"),
TypeName::Error => write!(fmt, "{{error}}"),
}
}
Expand Down
3 changes: 3 additions & 0 deletions chalk-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ pub enum TypeName<I: Interner> {
/// the string primitive type
Str,

/// the never type `!`
Never,

/// This can be used to represent an error, e.g. during name resolution of a type.
/// Chalk itself will not produce this, just pass it through when given.
Error,
Expand Down
1 change: 1 addition & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub enum Ty {
ty: Box<Ty>,
},
Str,
Never,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Expand Down
1 change: 1 addition & 0 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ TyWithoutId: Ty = {
},
<ScalarType> => Ty::Scalar { ty: <> },
"str" => Ty::Str,
"!" => Ty::Never,
"fn" "(" <t:Ty> ")" => Ty::ForAll {
lifetime_names: vec![],
ty: Box::new(t)
Expand Down
19 changes: 9 additions & 10 deletions chalk-solve/src/clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,19 @@ fn match_type_name<I: Interner>(
.db
.associated_ty_data(type_id)
.to_program_clauses(builder),
TypeName::Scalar(_) => {
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
}
TypeName::Str => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
TypeName::Tuple(_) => {
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
}
TypeName::Slice => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
TypeName::Raw(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
TypeName::Ref(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
TypeName::FnDef(fn_def_id) => builder
.db
.fn_def_datum(fn_def_id)
.to_program_clauses(builder),
TypeName::Tuple(_)
| TypeName::Scalar(_)
| TypeName::Str
| TypeName::Slice
| TypeName::Raw(_)
| TypeName::Ref(_)
| TypeName::Never => {
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
}
}
}

Expand Down
5 changes: 1 addition & 4 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, Mutability, Substitution, TyData, TypeName};
use chalk_ir::{ApplicationTy, Substitution, TyData, TypeName};

fn push_tuple_copy_conditions<I: Interner>(
db: &dyn RustIrDatabase<I>,
Expand Down Expand Up @@ -41,9 +41,6 @@ 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(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 chalk-solve/src/clauses/builtin_traits/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn add_sized_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_sized_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Scalar(_) | TypeName::Raw(_) | TypeName::Ref(_) => {
TypeName::Never | TypeName::Scalar(_) | TypeName::Raw(_) | TypeName::Ref(_) => {
builder.push_fact(trait_ref.clone())
}
_ => return,
Expand Down
1 change: 1 addition & 0 deletions tests/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ mod implied_bounds;
mod impls;
mod misc;
mod negation;
mod never;
mod object_safe;
mod projection;
mod refs;
Expand Down
13 changes: 13 additions & 0 deletions tests/test/never.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

#[test]
fn never_is_well_formed() {
test! {
program { }
goal {
WellFormed(!)
} yields {
"Unique"
}
}
}
64 changes: 0 additions & 64 deletions tests/test/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,67 +57,3 @@ fn mut_refs_are_sized() {
}
}
}

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

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

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

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

#[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 []"
}
}
}