Skip to content

Add reference types to TypeName #444

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 7 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
18 changes: 18 additions & 0 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,24 @@ impl LowerTy for Ty {
)?,
})
.intern(interner)),

Ty::Ref {
mutability,
lifetime,
ty,
} => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Ref(ast_mutability_to_chalk_mutability(
mutability.clone(),
)),
substitution: chalk_ir::Substitution::from(
interner,
&[
lifetime.lower(env)?.cast(interner),
ty.lower(env)?.cast(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 @@ -145,6 +145,7 @@ impl<I: Interner> Debug for TypeName<I> {
TypeName::Tuple(arity) => write!(fmt, "{:?}", arity),
TypeName::OpaqueType(opaque_ty) => write!(fmt, "!{:?}", opaque_ty),
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Ref(mutability) => write!(fmt, "{:?}", mutability),
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 @@ -159,6 +159,9 @@ pub enum TypeName<I: Interner> {
/// a raw pointer type like `*const T` or `*mut T`
Raw(Mutability),

/// a reference type like `&T` or `&mut T`
Ref(Mutability),

/// a placeholder for opaque types like `impl Trait`
OpaqueType(OpaqueTyId<I>),

Expand Down
5 changes: 5 additions & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ pub enum Ty {
mutability: Mutability,
ty: Box<Ty>,
},
Ref {
mutability: Mutability,
lifetime: Lifetime,
ty: Box<Ty>,
},
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Expand Down
6 changes: 4 additions & 2 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ TyWithoutFor: Ty = {
<n:Id> "<" <a:Comma<Parameter>> ">" => Ty::Apply { name: n, args: a },
<p:ProjectionTy> => Ty::Projection { proj: p },
"(" <t:TupleOrParensInner> ")" => t,
"*" <m: Mutability> <t:Ty> => Ty::Raw{ mutability: m, ty: Box::new(t) },
"*" <m: RawMutability> <t:Ty> => Ty::Raw{ mutability: m, ty: Box::new(t) },
"&" <l: Lifetime> "mut" <t:Ty> => Ty::Ref{ mutability: Mutability::Mut, lifetime: l, ty: Box::new(t) },
"&" <l: Lifetime> <t:Ty> => Ty::Ref{ mutability: Mutability::Not, lifetime: l, ty: Box::new(t) },
};

ScalarType: ScalarType = {
Expand Down Expand Up @@ -238,7 +240,7 @@ TupleOrParensInner: Ty = {
() => Ty::Tuple { types: vec![] },
};

Mutability: Mutability = {
RawMutability: Mutability = {
"mut" => Mutability::Mut,
"const" => Mutability::Not,
};
Expand Down
1 change: 1 addition & 0 deletions chalk-solve/src/clauses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ fn match_type_name<I: Interner>(
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))),
}
}

Expand Down
2 changes: 1 addition & 1 deletion chalk-solve/src/clauses/builtin_traits/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn add_copy_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_copy_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Raw(_) => builder.push_fact(trait_ref.clone()),
TypeName::Raw(_) | TypeName::Ref(_) => builder.push_fact(trait_ref.clone()),
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
5 changes: 3 additions & 2 deletions chalk-solve/src/clauses/builtin_traits/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ pub fn add_sized_program_clauses<I: Interner>(
TypeName::Struct(struct_id) => {
push_struct_sized_conditions(db, builder, trait_ref, *struct_id, substitution)
}
TypeName::Scalar(_) => builder.push_fact(trait_ref.clone()),
TypeName::Tuple(arity) => {
push_tuple_sized_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Raw(_) => builder.push_fact(trait_ref.clone()),
TypeName::Scalar(_) | TypeName::Raw(_) | TypeName::Ref(_) => {
builder.push_fact(trait_ref.clone())
}
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
30 changes: 27 additions & 3 deletions tests/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ fn scalars() {
}

error_msg {
"parse error: UnrecognizedToken { token: (8, Token(51, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
"parse error: UnrecognizedToken { token: (8, Token(52, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}
}
Expand All @@ -495,7 +495,7 @@ fn raw_pointers() {
struct *const i32 { }
}
error_msg {
"parse error: UnrecognizedToken { token: (8, Token(7, \"*\"), 9), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
"parse error: UnrecognizedToken { token: (8, Token(8, \"*\"), 9), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}

Expand All @@ -505,7 +505,31 @@ fn raw_pointers() {
impl Foo for *i32 { }
}
error_msg {
"parse error: UnrecognizedToken { token: (30, Token(51, \"i32\"), 33), expected: [\"\\\"const\\\"\", \"\\\"mut\\\"\"] }"
"parse error: UnrecognizedToken { token: (30, Token(52, \"i32\"), 33), expected: [\"\\\"const\\\"\", \"\\\"mut\\\"\"] }"
}
}
}

#[test]
fn refs() {
lowering_success! {
program {
trait Foo { }

impl<'a, T> Foo for &'a T { }
impl<'b, T> Foo for &'b mut T { }
}
}

lowering_error! {
program {
trait Foo { }

impl<T> Foo for &T { }
}

error_msg {
"parse error: UnrecognizedToken { token: (36, Token(1, \"T\"), 37), expected: [\"r#\\\"\\\\\\\'([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}
}
1 change: 1 addition & 0 deletions tests/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ mod impls;
mod misc;
mod negation;
mod projection;
mod refs;
mod scalars;
mod tuples;
mod unify;
Expand Down
62 changes: 62 additions & 0 deletions tests/test/refs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::*;

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

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

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

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

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

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

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

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