Skip to content

Add DiscriminantKind builtin trait #633

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 4 commits into from
Dec 10, 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
6 changes: 5 additions & 1 deletion chalk-integration/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
self.program_ir().unwrap().generator_witness_datum(id)
}

fn adt_repr(&self, id: AdtId<ChalkIr>) -> AdtRepr {
fn adt_repr(&self, id: AdtId<ChalkIr>) -> Arc<AdtRepr<ChalkIr>> {
self.program_ir().unwrap().adt_repr(id)
}

Expand Down Expand Up @@ -242,6 +242,10 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
fn fn_def_name(&self, fn_def_id: FnDefId<ChalkIr>) -> String {
self.program_ir().unwrap().fn_def_name(fn_def_id)
}

fn discriminant_type(&self, ty: Ty<ChalkIr>) -> Ty<ChalkIr> {
self.program_ir().unwrap().discriminant_type(ty)
}
}

impl fmt::Debug for ChalkDatabase {
Expand Down
80 changes: 53 additions & 27 deletions chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,15 @@ impl LowerWithEnv for (&AdtDefn, chalk_ir::AdtId<ChalkIr>) {
}
}

impl Lower for AdtRepr {
type Lowered = rust_ir::AdtRepr;
impl LowerWithEnv for AdtRepr {
type Lowered = rust_ir::AdtRepr<ChalkIr>;

fn lower(&self) -> Self::Lowered {
rust_ir::AdtRepr {
repr_c: self.repr_c,
repr_packed: self.repr_packed,
}
fn lower(&self, env: &Env) -> LowerResult<Self::Lowered> {
Ok(rust_ir::AdtRepr {
c: self.c,
packed: self.packed,
int: self.int.as_ref().map(|i| i.lower(env)).transpose()?,
})
}
}

Expand Down Expand Up @@ -1131,6 +1132,7 @@ impl Lower for WellKnownTrait {
WellKnownTrait::Unsize => rust_ir::WellKnownTrait::Unsize,
WellKnownTrait::Unpin => rust_ir::WellKnownTrait::Unpin,
WellKnownTrait::CoerceUnsized => rust_ir::WellKnownTrait::CoerceUnsized,
WellKnownTrait::DiscriminantKind => rust_ir::WellKnownTrait::DiscriminantKind,
}
}
}
Expand Down Expand Up @@ -1160,31 +1162,55 @@ impl Kinded for chalk_ir::GenericArg<ChalkIr> {
}
}

impl Lower for IntTy {
type Lowered = chalk_ir::IntTy;

fn lower(&self) -> Self::Lowered {
match self {
IntTy::I8 => chalk_ir::IntTy::I8,
IntTy::I16 => chalk_ir::IntTy::I16,
IntTy::I32 => chalk_ir::IntTy::I32,
IntTy::I64 => chalk_ir::IntTy::I64,
IntTy::I128 => chalk_ir::IntTy::I128,
IntTy::Isize => chalk_ir::IntTy::Isize,
}
}
}

impl Lower for UintTy {
type Lowered = chalk_ir::UintTy;

fn lower(&self) -> Self::Lowered {
match self {
UintTy::U8 => chalk_ir::UintTy::U8,
UintTy::U16 => chalk_ir::UintTy::U16,
UintTy::U32 => chalk_ir::UintTy::U32,
UintTy::U64 => chalk_ir::UintTy::U64,
UintTy::U128 => chalk_ir::UintTy::U128,
UintTy::Usize => chalk_ir::UintTy::Usize,
}
}
}

impl Lower for FloatTy {
type Lowered = chalk_ir::FloatTy;

fn lower(&self) -> Self::Lowered {
match self {
FloatTy::F32 => chalk_ir::FloatTy::F32,
FloatTy::F64 => chalk_ir::FloatTy::F64,
}
}
}

impl Lower for ScalarType {
type Lowered = chalk_ir::Scalar;

fn lower(&self) -> Self::Lowered {
match self {
ScalarType::Int(int) => chalk_ir::Scalar::Int(match int {
IntTy::I8 => chalk_ir::IntTy::I8,
IntTy::I16 => chalk_ir::IntTy::I16,
IntTy::I32 => chalk_ir::IntTy::I32,
IntTy::I64 => chalk_ir::IntTy::I64,
IntTy::I128 => chalk_ir::IntTy::I128,
IntTy::Isize => chalk_ir::IntTy::Isize,
}),
ScalarType::Uint(uint) => chalk_ir::Scalar::Uint(match uint {
UintTy::U8 => chalk_ir::UintTy::U8,
UintTy::U16 => chalk_ir::UintTy::U16,
UintTy::U32 => chalk_ir::UintTy::U32,
UintTy::U64 => chalk_ir::UintTy::U64,
UintTy::U128 => chalk_ir::UintTy::U128,
UintTy::Usize => chalk_ir::UintTy::Usize,
}),
ScalarType::Float(float) => chalk_ir::Scalar::Float(match float {
FloatTy::F32 => chalk_ir::FloatTy::F32,
FloatTy::F64 => chalk_ir::FloatTy::F64,
}),
ScalarType::Int(int) => chalk_ir::Scalar::Int(int.lower()),
ScalarType::Uint(uint) => chalk_ir::Scalar::Uint(uint.lower()),
ScalarType::Float(float) => chalk_ir::Scalar::Float(float.lower()),
ScalarType::Bool => chalk_ir::Scalar::Bool,
ScalarType::Char => chalk_ir::Scalar::Char,
}
Expand Down
2 changes: 1 addition & 1 deletion chalk-integration/src/lowering/program_lowerer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl ProgramLowerer {
let identifier = d.name.clone();
let adt_id = AdtId(raw_id);
adt_data.insert(adt_id, Arc::new((d, adt_id).lower(&empty_env)?));
adt_reprs.insert(adt_id, d.repr.lower());
adt_reprs.insert(adt_id, Arc::new(d.repr.lower(&empty_env)?));
let n_params = d.all_parameters().len();
let variances = match d.variances.clone() {
Some(v) => {
Expand Down
27 changes: 21 additions & 6 deletions chalk-integration/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use chalk_ir::{could_match::CouldMatch, UnificationDatabase};
use chalk_ir::{debug::Angle, Variance};
use chalk_ir::{
debug::SeparatorTraitRef, AdtId, AliasTy, AssocTypeId, Binders, CanonicalVarKinds, ClosureId,
FnDefId, ForeignDefId, GeneratorId, GenericArg, Goal, Goals, ImplId, Lifetime, OpaqueTy,
OpaqueTyId, ProgramClause, ProgramClauseImplication, ProgramClauses, ProjectionTy,
Substitution, TraitId, Ty, TyKind, Variances,
FnDefId, ForeignDefId, GeneratorId, GenericArg, Goal, Goals, ImplId, IntTy, Lifetime, OpaqueTy,
OpaqueTyId, ProgramClause, ProgramClauseImplication, ProgramClauses, ProjectionTy, Scalar,
Substitution, TraitId, Ty, TyKind, UintTy, Variances,
};
use chalk_solve::rust_ir::{
AdtDatum, AdtRepr, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind,
Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct Program {
/// For each ADT:
pub adt_data: BTreeMap<AdtId<ChalkIr>, Arc<AdtDatum<ChalkIr>>>,

pub adt_reprs: BTreeMap<AdtId<ChalkIr>, AdtRepr>,
pub adt_reprs: BTreeMap<AdtId<ChalkIr>, Arc<AdtRepr<ChalkIr>>>,

pub fn_def_data: BTreeMap<FnDefId<ChalkIr>, Arc<FnDefDatum<ChalkIr>>>,

Expand Down Expand Up @@ -426,8 +426,8 @@ impl RustIrDatabase<ChalkIr> for Program {
self.generator_witness_data[&id].clone()
}

fn adt_repr(&self, id: AdtId<ChalkIr>) -> AdtRepr {
self.adt_reprs[&id]
fn adt_repr(&self, id: AdtId<ChalkIr>) -> Arc<AdtRepr<ChalkIr>> {
self.adt_reprs[&id].clone()
}

fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
Expand Down Expand Up @@ -589,4 +589,19 @@ impl RustIrDatabase<ChalkIr> for Program {
.name
.to_string()
}

// Mirrors current (07a63e6d1fabf3560e8e1e17c1d56b10a06152d9) implementation in rustc
fn discriminant_type(&self, ty: Ty<ChalkIr>) -> Ty<ChalkIr> {
let interner = self.interner();
match ty.data(interner).kind {
TyKind::Adt(id, _) => {
let repr = self.adt_repr(id);
repr.int
.clone()
.unwrap_or(TyKind::Scalar(Scalar::Int(IntTy::Isize)).intern(interner))
}
TyKind::Generator(..) => TyKind::Scalar(Scalar::Uint(UintTy::U32)).intern(interner),
_ => TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(interner),
}
}
}
13 changes: 11 additions & 2 deletions chalk-parse/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,18 @@ pub enum AdtKind {
Union,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum AdtReprAttr {
C,
Packed,
Int(Ty),
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AdtRepr {
pub repr_c: bool,
pub repr_packed: bool,
pub c: bool,
pub packed: bool,
pub int: Option<Ty>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -143,6 +151,7 @@ pub enum WellKnownTrait {
Unsize,
Unpin,
CoerceUnsized,
DiscriminantKind,
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
83 changes: 60 additions & 23 deletions chalk-parse/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,31 @@ WellKnownTrait: WellKnownTrait = {
"#" "[" "lang" "(" "unsize" ")" "]" => WellKnownTrait::Unsize,
"#" "[" "lang" "(" "unpin" ")" "]" => WellKnownTrait::Unpin,
"#" "[" "lang" "(" "coerce_unsized" ")" "]" => WellKnownTrait::CoerceUnsized,
"#" "[" "lang" "(" "discriminant_kind" ")" "]" => WellKnownTrait::DiscriminantKind,
};

AdtRepr: Atom = "#" "[" "repr" "(" <name:Id> ")" "]" => name.str;
AdtReprAttr: AdtReprAttr = {
"#" "[" "repr" "(" <t:ReprIntTy> ")" "]" => AdtReprAttr::Int(t),
"#" "[" "repr" "(" <attr:Id> ")" "]" =>? match &*attr.str {
"C" => Ok(AdtReprAttr::C),
"packed" => Ok(AdtReprAttr::Packed),
_ => Err(lalrpop_util::ParseError::User {
error: "unknown adt repr flag"
})
},
};

ReprIntTy: Ty = {
<i:IntTy> => Ty::Scalar {
ty: ScalarType::Int(i),
},
<u:UintTy> => Ty::Scalar {
ty: ScalarType::Uint(u),
},
}

AdtDefn: AdtDefn = {
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtRepr*>
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtReprAttr*>
"enum" <n:Id><p:Angle<VariableKind>>
<w:QuantifiedWhereClauses> "{" <v:Variants> "}" => AdtDefn
{
Expand All @@ -85,12 +104,17 @@ AdtDefn: AdtDefn = {
kind: AdtKind::Enum,
},
repr: AdtRepr {
repr_c: repr.iter().any(|s| s == "C"),
repr_packed: repr.iter().any(|s| s == "packed"),
c: repr.iter().any(|s| s == &AdtReprAttr::C),
packed: repr.iter().any(|s| s == &AdtReprAttr::Packed),
int: repr.iter().find_map(|s| if let AdtReprAttr::Int(i) = s {
Some(i.clone())
} else {
None
})
},
variances,
},
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtRepr*>
<variances:Variances?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <phantom_data:PhantomDataKeyword?> <repr:AdtReprAttr*>
"struct" <n:Id><p:Angle<VariableKind>>
<w:QuantifiedWhereClauses> "{" <f:Fields> "}" => AdtDefn
{
Expand All @@ -112,8 +136,9 @@ AdtDefn: AdtDefn = {
kind: AdtKind::Struct,
},
repr: AdtRepr {
repr_c: repr.iter().any(|s| s == "C"),
repr_packed: repr.iter().any(|s| s == "packed"),
c: repr.iter().any(|s| s == &AdtReprAttr::C),
packed: repr.iter().any(|s| s == &AdtReprAttr::Packed),
int: None
},
variances,
}
Expand Down Expand Up @@ -405,23 +430,35 @@ TyWithoutId: Ty = {
ExistsLifetimes: Vec<Identifier> = "exists" "<" <Comma<LifetimeId>> ">" => <>;
ForLifetimes: Vec<Identifier> = "for" "<" <Comma<LifetimeId>> ">" => <>;

IntTy: IntTy = {
"i8" => IntTy::I8,
"i16" => IntTy::I16,
"i32" => IntTy::I32,
"i64" => IntTy::I64,
"i128" => IntTy::I128,
"isize" => IntTy::Isize,
};

UintTy: UintTy = {
"u8" => UintTy::U8,
"u16" => UintTy::U16,
"u32" => UintTy::U32,
"u64" => UintTy::U64,
"u128" => UintTy::U128,
"usize" => UintTy::Usize,
};

FloatTy: FloatTy = {
"f32" => FloatTy::F32,
"f64" => FloatTy::F64,
};

ScalarType: ScalarType = {
"u8" => ScalarType::Uint(UintTy::U8),
"u16" => ScalarType::Uint(UintTy::U16),
"u32" => ScalarType::Uint(UintTy::U32),
"u64" => ScalarType::Uint(UintTy::U64),
"u128" => ScalarType::Uint(UintTy::U128),
"usize" => ScalarType::Uint(UintTy::Usize),
"i8" => ScalarType::Int(IntTy::I8),
"i16" => ScalarType::Int(IntTy::I16),
"i32" => ScalarType::Int(IntTy::I32),
"i64" => ScalarType::Int(IntTy::I64),
"i128" => ScalarType::Int(IntTy::I128),
"isize" => ScalarType::Int(IntTy::Isize),
"f32" => ScalarType::Float(FloatTy::F32),
"f64" => ScalarType::Float(FloatTy::F64),
"bool" => ScalarType::Bool,
"char" => ScalarType::Char,
<i:IntTy> => ScalarType::Int(i),
<u:UintTy> => ScalarType::Uint(u),
<f:FloatTy> => ScalarType::Float(f),
"bool" => ScalarType::Bool,
"char" => ScalarType::Char,
};

TupleOrParensInner: Ty = {
Expand Down
6 changes: 6 additions & 0 deletions chalk-solve/src/clauses/builtin_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use chalk_ir::{Floundered, Substitution, Ty};

mod clone;
mod copy;
mod discriminant_kind;
mod fn_family;
mod sized;
mod unsize;
Expand Down Expand Up @@ -41,6 +42,8 @@ pub fn add_builtin_program_clauses<I: Interner>(
WellKnownTrait::Unsize => {
unsize::add_unsize_program_clauses(db, builder, trait_ref, ty)
}
// DiscriminantKind is automatically implemented for all types
WellKnownTrait::DiscriminantKind => builder.push_fact(trait_ref),
// There are no builtin impls provided for the following traits:
WellKnownTrait::Unpin | WellKnownTrait::Drop | WellKnownTrait::CoerceUnsized => (),
}
Expand All @@ -67,6 +70,9 @@ pub fn add_builtin_assoc_program_clauses<I: Interner>(
Ok(())
})
}
WellKnownTrait::DiscriminantKind => {
discriminant_kind::add_discriminant_clauses(db, builder, self_ty)
}
_ => Ok(()),
}
}
Expand Down
Loading