Skip to content

Add raw pointers to TypeName #442

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 9 commits into from
May 7, 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
1 change: 1 addition & 0 deletions book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Some common examples of auto traits are `Send` and `Sync`.
| scalar types | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| trait objects | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ |
| functions ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| raw ptrs | ✅ | ✅ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| arrays❌ | ❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| slices❌ | ❌ | ❌ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| closures❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
Expand Down
18 changes: 18 additions & 0 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,17 @@ impl LowerTy for Ty {
substitution: chalk_ir::Substitution::empty(interner),
})
.intern(interner)),

Ty::Raw { mutability, ty } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Raw(ast_mutability_to_chalk_mutability(
mutability.clone(),
)),
substitution: chalk_ir::Substitution::from_fallible(
interner,
std::iter::once(Ok(ty.lower(env)?)),
)?,
})
.intern(interner)),
}
}
}
Expand Down Expand Up @@ -1591,3 +1602,10 @@ fn ast_scalar_to_chalk_scalar(scalar: ScalarType) -> chalk_ir::Scalar {
ScalarType::Char => chalk_ir::Scalar::Char,
}
}

fn ast_mutability_to_chalk_mutability(mutability: Mutability) -> chalk_ir::Mutability {
match mutability {
Mutability::Mut => chalk_ir::Mutability::Mut,
Mutability::Not => chalk_ir::Mutability::Not,
}
}
1 change: 1 addition & 0 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl<I: Interner> Debug for TypeName<I> {
TypeName::Scalar(scalar) => write!(fmt, "{:?}", scalar),
TypeName::Tuple(arity) => write!(fmt, "{:?}", arity),
TypeName::OpaqueType(opaque_ty) => write!(fmt, "!{:?}", opaque_ty),
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Error => write!(fmt, "{{error}}"),
}
}
Expand Down
1 change: 1 addition & 0 deletions chalk-ir/src/fold/boring_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ copy_fold!(IntTy);
copy_fold!(FloatTy);
copy_fold!(Scalar);
copy_fold!(ClausePriority);
copy_fold!(Mutability);

#[macro_export]
macro_rules! id_fold {
Expand Down
9 changes: 9 additions & 0 deletions chalk-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ pub enum Scalar {
Float(FloatTy),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Mutability {
Mut,
Not,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Fold, Visit)]
pub enum TypeName<I: Interner> {
/// a type like `Vec<T>`
Expand All @@ -150,6 +156,9 @@ pub enum TypeName<I: Interner> {
/// a tuple of the given arity
Tuple(usize),

/// a raw pointer type like `*const T` or `*mut T`
Raw(Mutability),

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

Expand Down
7 changes: 4 additions & 3 deletions chalk-ir/src/visit/boring_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use crate::{
AssocTypeId, ClausePriority, DebruijnIndex, FloatTy, Goals, ImplId, IntTy, Interner,
OpaqueTyId, Parameter, ParameterKind, PlaceholderIndex, ProgramClause, ProgramClauseData,
ProgramClauses, QuantifiedWhereClauses, QuantifierKind, Scalar, StructId, Substitution,
SuperVisit, TraitId, UintTy, UniverseIndex, Visit, VisitResult, Visitor,
Mutability, OpaqueTyId, Parameter, ParameterKind, PlaceholderIndex, ProgramClause,
ProgramClauseData, ProgramClauses, QuantifiedWhereClauses, QuantifierKind, Scalar, StructId,
Substitution, SuperVisit, TraitId, UintTy, UniverseIndex, Visit, VisitResult, Visitor,
};
use chalk_engine::{context::Context, ExClause, FlounderedSubgoal, Literal};
use std::{marker::PhantomData, sync::Arc};
Expand Down Expand Up @@ -211,6 +211,7 @@ const_visit!(Scalar);
const_visit!(UintTy);
const_visit!(IntTy);
const_visit!(FloatTy);
const_visit!(Mutability);

#[macro_export]
macro_rules! id_visit {
Expand Down
10 changes: 10 additions & 0 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ pub enum Ty {
Scalar {
ty: ScalarType,
},
Raw {
mutability: Mutability,
ty: Box<Ty>,
},
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -230,6 +234,12 @@ pub enum ScalarType {
Float(FloatTy),
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Mutability {
Mut,
Not,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Lifetime {
Id { name: Identifier },
Expand Down
6 changes: 6 additions & 0 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ 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) },
};

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

Mutability: Mutability = {
"mut" => Mutability::Mut,
"const" => Mutability::Not,
};

Lifetime: Lifetime = {
<n:LifetimeId> => Lifetime::Id { name: n },
};
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 @@ -410,6 +410,7 @@ fn match_type_name<I: Interner>(
TypeName::Tuple(_) => {
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
}
TypeName::Raw(_) => builder.push_fact(WellFormed::Ty(application.clone().intern(interner))),
}
}

Expand Down
1 change: 1 addition & 0 deletions chalk-solve/src/clauses/builtin_traits/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +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()),
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
1 change: 1 addition & 0 deletions chalk-solve/src/clauses/builtin_traits/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub fn add_sized_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_sized_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Raw(_) => builder.push_fact(trait_ref.clone()),
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
36 changes: 35 additions & 1 deletion tests/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,41 @@ fn scalars() {
}

error_msg {
"parse error: UnrecognizedToken { token: (8, Token(49, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
"parse error: UnrecognizedToken { token: (8, Token(51, \"i32\"), 11), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}
}

#[test]
fn raw_pointers() {
lowering_success! {
program {
trait Quux { }
struct Foo<T> { a: *const T }

struct Bar<T> { a: *mut T }

impl<T> Quux for Foo<*mut T> { }
impl<T> Quux for Bar<*const T> { }
}
}

lowering_error! {
program {
struct *const i32 { }
}
error_msg {
"parse error: UnrecognizedToken { token: (8, Token(7, \"*\"), 9), expected: [\"r#\\\"([A-Za-z]|_)([A-Za-z0-9]|_)*\\\"#\"] }"
}
}

lowering_error! {
program {
trait Foo { }
impl Foo for *i32 { }
}
error_msg {
"parse error: UnrecognizedToken { token: (30, Token(51, \"i32\"), 33), expected: [\"\\\"const\\\"\", \"\\\"mut\\\"\"] }"
}
}
}