Skip to content

Add array types to TypeName #467

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 26, 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
2 changes: 1 addition & 1 deletion book/src/clauses/well_known_traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Some common examples of auto traits are `Send` and `Sync`.
| immutable refs | 📚 | 📚 | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| mutable refs | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| slices | ⚬ | ⚬ | ⚬ | ✅ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| arrays | ❌ | | | | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| arrays | ✅ | | | | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
| closures❌ | ❌ | ❌ | ❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ❌ |
| generators❌ | ⚬ | ⚬ | ❌ | ⚬ | ⚬ | ⚬ | ❌ | ❌ | ❌ |
| gen. witness❌ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ⚬ | ❌ |
Expand Down
51 changes: 43 additions & 8 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,18 @@ impl LowerTy for Ty {
})
.intern(interner)),

Ty::Array { ty, len } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Array,
substitution: chalk_ir::Substitution::from(
interner,
&[
ty.lower(env)?.cast(interner),
len.lower(env)?.cast(interner),
],
),
})
.intern(interner)),

Ty::Slice { ty } => Ok(chalk_ir::TyData::Apply(chalk_ir::ApplicationTy {
name: chalk_ir::TypeName::Slice,
substitution: chalk_ir::Substitution::from_fallible(
Expand Down Expand Up @@ -1448,6 +1460,36 @@ impl LowerTy for Ty {
}
}

trait LowerConst {
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Const<ChalkIr>>;
}

impl LowerConst for Const {
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Const<ChalkIr>> {
let interner = env.interner();
match self {
Const::Id(name) => {
let parameter = env.lookup_generic_arg(name)?;
parameter
.constant(interner)
.ok_or_else(|| RustIrError::IncorrectParameterKind {
identifier: name.clone(),
expected: Kind::Const,
actual: parameter.kind(),
})
.map(|c| c.clone())
}
Const::Value(value) => Ok(chalk_ir::ConstData {
ty: get_type_of_u32(),
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
interned: value.clone(),
}),
}
.intern(interner)),
}
}
}

trait LowerGenericArg {
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::GenericArg<ChalkIr>>;
}
Expand All @@ -1459,14 +1501,7 @@ impl LowerGenericArg for GenericArg {
GenericArg::Ty(ref t) => Ok(t.lower(env)?.cast(interner)),
GenericArg::Lifetime(ref l) => Ok(l.lower(env)?.cast(interner)),
GenericArg::Id(name) => env.lookup_generic_arg(&name),
GenericArg::ConstValue(value) => Ok(chalk_ir::ConstData {
ty: get_type_of_u32(),
value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst {
interned: value.clone(),
}),
}
.intern(interner)
.cast(interner)),
GenericArg::Const(c) => Ok(c.lower(env)?.cast(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 @@ -168,6 +168,7 @@ impl<I: Interner> Debug for TypeName<I> {
TypeName::Raw(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Ref(mutability) => write!(fmt, "{:?}", mutability),
TypeName::Never => write!(fmt, "Never"),
TypeName::Array => write!(fmt, "{{array}}"),
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 @@ -157,6 +157,9 @@ pub enum TypeName<I: Interner> {
/// a tuple of the given arity
Tuple(usize),

/// an array type like `[T; N]`
Array,

/// a slice type like `[T]`
Slice,

Expand Down
12 changes: 11 additions & 1 deletion chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ pub enum GenericArg {
Ty(Ty),
Lifetime(Lifetime),
Id(Identifier),
ConstValue(u32),
Const(Const),
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Const {
Id(Identifier),
Value(u32),
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -212,6 +218,10 @@ pub enum Ty {
Slice {
ty: Box<Ty>,
},
Array {
ty: Box<Ty>,
len: Const,
},
Raw {
mutability: Mutability,
ty: Box<Ty>,
Expand Down
12 changes: 11 additions & 1 deletion chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ TyWithoutId: Ty = {
"&" <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) },
"[" <t:Ty> "]" => Ty::Slice { ty: Box::new(t) },
"[" <t:Ty> ";" <len:Const> "]" => Ty::Array { ty: Box::new(t), len },
};

ScalarType: ScalarType = {
Expand Down Expand Up @@ -280,11 +281,20 @@ Lifetime: Lifetime = {
<n:LifetimeId> => Lifetime::Id { name: n },
};

ConstWithoutId: Const = {
ConstValue => Const::Value(<>),
};

Const : Const = {
Id => Const::Id(<>),
ConstWithoutId,
};

GenericArg: GenericArg = {
TyWithoutId => GenericArg::Ty(<>),
Lifetime => GenericArg::Lifetime(<>),
Id => GenericArg::Id(<>),
ConstValue => GenericArg::ConstValue(<>),
ConstWithoutId => GenericArg::Const(<>),
};

ProjectionTy: ProjectionTy = {
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 @@ -421,6 +421,7 @@ fn match_type_name<I: Interner>(
| TypeName::Slice
| TypeName::Raw(_)
| TypeName::Ref(_)
| TypeName::Array
| TypeName::Never => {
builder.push_fact(WellFormed::Ty(application.clone().intern(interner)))
}
Expand Down
10 changes: 10 additions & 0 deletions chalk-solve/src/clauses/builtin_traits/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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 std::iter;

fn push_tuple_copy_conditions<I: Interner>(
db: &dyn RustIrDatabase<I>,
Expand Down Expand Up @@ -41,6 +42,15 @@ pub fn add_copy_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_copy_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Array => {
let interner = db.interner();
needs_impl_for_tys(
db,
builder,
trait_ref,
iter::once(substitution.at(interner, 0).assert_ty_ref(interner).clone()),
);
}
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
8 changes: 5 additions & 3 deletions chalk-solve/src/clauses/builtin_traits/sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ pub fn add_sized_program_clauses<I: Interner>(
TypeName::Tuple(arity) => {
push_tuple_sized_conditions(db, builder, trait_ref, *arity, substitution)
}
TypeName::Never | TypeName::Scalar(_) | TypeName::Raw(_) | TypeName::Ref(_) => {
builder.push_fact(trait_ref.clone())
}
TypeName::Array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have "WF" condition code somewhere? We'll have to check that the [T;N] requires T: Sized

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We push WF clauses in match_type_name, and I don't think we had a big discussion about how we want to handle well-formedness of tuples and arrays. IIRC the takeaway was that we rely on rustc to provide well-formed tuples (and, in this case, arrays), but I might be wrong

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was thinking of WfSolver -- but yeah I think we probably shouldn't block on this right now, it's something we'll definitely want to think about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just file an issue I guess.

| TypeName::Never
| TypeName::Scalar(_)
| TypeName::Raw(_)
| TypeName::Ref(_) => builder.push_fact(trait_ref.clone()),
_ => return,
},
TyData::Function(_) => builder.push_fact(trait_ref.clone()),
Expand Down
93 changes: 93 additions & 0 deletions tests/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,52 @@ fn check_variable_kinds() {
"incorrect parameter kind for trait `IntoTime`: expected lifetime, found type"
}
}

lowering_error! {
program {
trait Length<const N> {}
struct Foo {}
impl<T> Length<T> for Foo {}
}
error_msg {
"incorrect parameter kind for trait `Length`: expected const, found type"
}
}

lowering_error! {
program {
trait Length<const N> {}
struct Foo {}
impl<'a> Length<'a> for Foo {}
}
error_msg {
"incorrect parameter kind for trait `Length`: expected const, found lifetime"
}
}

lowering_error! {
program {
trait Into<T> {}
struct Foo {}
impl<const N> Into<N> for Foo {}
}

error_msg {
"incorrect parameter kind for trait `Into`: expected type, found const"
}
}

lowering_error! {
program {
trait IntoTime<'a> {}
struct Foo {}
impl<const N> IntoTime<N> for Foo {}
}

error_msg {
"incorrect parameter kind for trait `IntoTime`: expected lifetime, found const"
}
}
}

#[test]
Expand Down Expand Up @@ -584,3 +630,50 @@ fn fn_defs() {
}
}
}
#[test]
fn arrays() {
lowering_success! {
program {
struct Baz { }
fn foo(bar: [Baz; 3]);

fn bar<const N>(baz: [Baz; N]);
}
}

lowering_error! {
program {
struct Baz { }

fn foo<T>(baz: [Baz; u32]);
}

error_msg {
"parse error: UnrecognizedToken"
}
}

lowering_error! {
program {
struct Baz { }

fn foo<T>(baz: [Baz; T]);
}

error_msg {
"incorrect parameter kind for `T`: expected const, found type"
}
}

lowering_error! {
program {
struct Baz { }

fn foo<'a>(baz: [Baz; 'a]);
}

error_msg {
"parse error: UnrecognizedToken"
}
}
}
Loading