Skip to content

Commit

Permalink
frontend: Get enumerate() test to typecheck successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Jan 30, 2025
1 parent a3a1309 commit 2f301db
Show file tree
Hide file tree
Showing 19 changed files with 178 additions and 12 deletions.
1 change: 1 addition & 0 deletions dora-bytecode/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub enum BytecodeTypeKind {
Lambda,
TypeAlias,
Assoc,
GenericAssoc,
This,
}

Expand Down
4 changes: 3 additions & 1 deletion dora-bytecode/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ impl<'a> BytecodeTypePrinter<'a> {

BytecodeType::This => write!(fmt, "Self"),

BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => unimplemented!(),
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. } => unimplemented!(),

BytecodeType::Lambda(params, return_type) => {
write!(fmt, "(")?;
Expand Down
1 change: 1 addition & 0 deletions dora-bytecode/src/dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ impl<'a> Display for BytecodeTypePrinter<'a> {
Ok(())
}
}
BytecodeType::GenericAssoc { .. } => unimplemented!(),
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion dora-bytecode/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub enum BytecodeType {
Lambda(BytecodeTypeArray, Box<BytecodeType>),
TypeAlias(AliasId),
Assoc(AliasId, BytecodeTypeArray),
GenericAssoc {
type_param_id: u32,
trait_id: TraitId,
assoc_id: AliasId,
},
}

impl BytecodeType {
Expand All @@ -48,6 +53,7 @@ impl BytecodeType {
BytecodeType::Lambda(..) => BytecodeTypeKind::Lambda,
BytecodeType::TypeAlias(..) => BytecodeTypeKind::TypeAlias,
BytecodeType::Assoc(..) => BytecodeTypeKind::Assoc,
BytecodeType::GenericAssoc { .. } => BytecodeTypeKind::GenericAssoc,
BytecodeType::This => unreachable!(),
}
}
Expand Down Expand Up @@ -153,7 +159,10 @@ impl BytecodeType {
params.is_concrete_type() && return_type.is_concrete_type()
}
BytecodeType::TypeParam(_) => false,
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) | BytecodeType::This => {
BytecodeType::TypeAlias(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::Assoc(..)
| BytecodeType::This => {
unreachable!()
}
}
Expand Down
18 changes: 18 additions & 0 deletions dora-frontend/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,15 @@ pub fn bty_from_ty(ty: SourceType) -> BytecodeType {
bty_array_from_ty(&type_params),
)
}
SourceType::GenericAssoc {
tp_id,
trait_id,
assoc_id,
} => BytecodeType::GenericAssoc {
type_param_id: tp_id.index().try_into().expect("overflow"),
trait_id: TraitId(trait_id.index().try_into().expect("overflow")),
assoc_id: AliasId(assoc_id.index().try_into().expect("overflow")),
},
_ => panic!("SourceType {:?} cannot be converted to BytecodeType", ty),
}
}
Expand Down Expand Up @@ -3467,6 +3476,15 @@ pub fn register_bty_from_ty(ty: SourceType) -> BytecodeType {
SourceType::Lambda(_, _) => BytecodeType::Ptr,
SourceType::Ptr => BytecodeType::Ptr,
SourceType::This => BytecodeType::This,
SourceType::GenericAssoc {
tp_id,
trait_id,
assoc_id,
} => BytecodeType::GenericAssoc {
type_param_id: tp_id.index().try_into().expect("overflow"),
trait_id: TraitId(trait_id.index().try_into().expect("overflow")),
assoc_id: AliasId(assoc_id.index().try_into().expect("overflow")),
},
_ => panic!("SourceType {:?} cannot be converted to BytecodeType", ty),
}
}
Expand Down
4 changes: 2 additions & 2 deletions dora-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub use parsety::{ParsedTraitType, ParsedType, ParsedTypeAst};
pub use path::{parse_path, PathKind};
pub use program_emitter::emit_program;
pub use specialize::{
replace_type, specialize_for_element, specialize_trait_type, specialize_ty_for_trait_object,
specialize_type, specialize_type_array,
replace_type, specialize_for_element, specialize_trait_type, specialize_ty_for_generic,
specialize_ty_for_trait_object, specialize_type, specialize_type_array,
};

pub(crate) mod access;
Expand Down
1 change: 1 addition & 0 deletions dora-frontend/src/sema/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use dora_parser::ast;

pub type AliasDefinitionId = Id<AliasDefinition>;

#[derive(PartialEq, Eq, Debug)]
pub enum AliasParent {
None,
Trait(TraitDefinitionId),
Expand Down
93 changes: 92 additions & 1 deletion dora-frontend/src/specialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::sema::{Element, Sema, TraitDefinitionId};
use crate::sema::{AliasParent, Element, Sema, TraitDefinitionId, TypeParamId};
use crate::{SourceType, SourceTypeArray, TraitType};

pub fn specialize_trait_type(sa: &Sema, ty: TraitType, type_params: &SourceTypeArray) -> TraitType {
Expand Down Expand Up @@ -243,6 +243,97 @@ fn specialize_ty_for_trait_object_array(
SourceTypeArray::with(new_array)
}

pub fn specialize_ty_for_generic(
sa: &Sema,
ty: SourceType,
type_param_id: TypeParamId,
trait_id: TraitDefinitionId,
) -> SourceType {
match ty {
SourceType::Class(cls_id, cls_type_params) => SourceType::Class(
cls_id,
specialize_ty_for_generic_array(sa, cls_type_params, type_param_id, trait_id),
),

SourceType::TraitObject(trait_id, trait_type_params, bindings) => SourceType::TraitObject(
trait_id,
specialize_ty_for_generic_array(sa, trait_type_params, type_param_id, trait_id),
specialize_ty_for_generic_array(sa, bindings, type_param_id, trait_id),
),

SourceType::Struct(struct_id, struct_type_params) => SourceType::Struct(
struct_id,
specialize_ty_for_generic_array(sa, struct_type_params, type_param_id, trait_id),
),

SourceType::Enum(enum_id, enum_type_params) => SourceType::Enum(
enum_id,
specialize_ty_for_generic_array(sa, enum_type_params, type_param_id, trait_id),
),

SourceType::Alias(alias_id, alias_type_params) => SourceType::Alias(
alias_id,
specialize_ty_for_generic_array(sa, alias_type_params, type_param_id, trait_id),
),

SourceType::Assoc(alias_id, alias_type_params) => {
let alias = sa.alias(alias_id);
assert_eq!(alias.parent, AliasParent::Trait(trait_id));
assert!(alias_type_params.is_empty());
SourceType::GenericAssoc {
tp_id: type_param_id,
trait_id,
assoc_id: alias_id,
}
}

SourceType::Lambda(params, return_type) => SourceType::Lambda(
specialize_ty_for_generic_array(sa, params, type_param_id, trait_id),
Box::new(specialize_ty_for_generic(
sa,
*return_type,
type_param_id,
trait_id,
)),
),

SourceType::Tuple(subtypes) => SourceType::Tuple(specialize_ty_for_generic_array(
sa,
subtypes,
type_param_id,
trait_id,
)),

SourceType::Unit
| SourceType::UInt8
| SourceType::Bool
| SourceType::Char
| SourceType::Int32
| SourceType::Int64
| SourceType::Float32
| SourceType::Float64
| SourceType::Error
| SourceType::TypeParam(..) => ty,

SourceType::This | SourceType::Any | SourceType::Ptr | SourceType::GenericAssoc { .. } => {
unreachable!()
}
}
}

fn specialize_ty_for_generic_array(
sa: &Sema,
array: SourceTypeArray,
type_param_id: TypeParamId,
trait_id: TraitDefinitionId,
) -> SourceTypeArray {
let new_array = array
.iter()
.map(|ty| specialize_ty_for_generic(sa, ty, type_param_id, trait_id))
.collect::<Vec<_>>();
SourceTypeArray::with(new_array)
}

pub fn specialize_for_element(
sa: &Sema,
ty: SourceType,
Expand Down
7 changes: 5 additions & 2 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::typeck::{
};
use crate::typeparamck;
use crate::{
empty_sta, specialize_ty_for_trait_object, specialize_type, ty::error as ty_error,
ErrorMessage, SourceType, SourceTypeArray, TraitType,
empty_sta, specialize_ty_for_generic, specialize_ty_for_trait_object, specialize_type,
ty::error as ty_error, ErrorMessage, SourceType, SourceTypeArray, TraitType,
};

pub(super) fn check_expr_call(
Expand Down Expand Up @@ -948,6 +948,9 @@ fn check_expr_call_generic_type_param(
Some(object_type.clone()),
);

let return_type =
specialize_ty_for_generic(ck.sa, return_type, id, trait_method.trait_id());

ck.analysis.set_ty(e.id, return_type.clone());

let trait_type_params = trait_ty.type_params;
Expand Down
4 changes: 3 additions & 1 deletion dora-runtime/src/boots/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ pub fn decode_bytecode_type(reader: &mut ByteReader) -> BytecodeType {
BytecodeType::Lambda(params, Box::new(return_ty))
}

BytecodeTypeKind::TypeAlias | BytecodeTypeKind::Assoc => unreachable!(),
BytecodeTypeKind::TypeAlias
| BytecodeTypeKind::Assoc
| BytecodeTypeKind::GenericAssoc { .. } => unreachable!(),
}
}

Expand Down
6 changes: 5 additions & 1 deletion dora-runtime/src/boots/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ fn encode_bytecode_type(vm: &VM, ty: &BytecodeType, buffer: &mut ByteBuffer) {
encode_bytecode_type_array(vm, params, buffer);
encode_bytecode_type(vm, ret.as_ref(), buffer);
}
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => unreachable!(),
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. } => {
unreachable!()
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions dora-runtime/src/cannon/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl<'a> BaselineAssembler<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::This => {
unreachable!()
Expand Down Expand Up @@ -311,6 +312,7 @@ impl<'a> BaselineAssembler<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Lambda(_, _)
| BytecodeType::This => {
Expand Down Expand Up @@ -418,6 +420,7 @@ impl<'a> BaselineAssembler<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Lambda(_, _)
| BytecodeType::This => {
Expand Down Expand Up @@ -1296,6 +1299,7 @@ impl<'a> BaselineAssembler<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::This => {
unreachable!()
Expand Down
10 changes: 10 additions & 0 deletions dora-runtime/src/cannon/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Class(_, _)
| BytecodeType::Lambda(_, _)
Expand Down Expand Up @@ -369,6 +370,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Unit
| BytecodeType::This => {
Expand Down Expand Up @@ -1589,6 +1591,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::Class(..)
| BytecodeType::TypeParam(_)
| BytecodeType::Struct(..)
Expand Down Expand Up @@ -1765,6 +1768,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Class(_, _)
| BytecodeType::Lambda(_, _)
Expand Down Expand Up @@ -2312,6 +2316,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Class(_, _)
| BytecodeType::Lambda(_, _)
Expand Down Expand Up @@ -3900,6 +3905,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Class(..)
| BytecodeType::Lambda(..)
Expand Down Expand Up @@ -4641,6 +4647,7 @@ pub fn result_passed_as_argument(ty: BytecodeType) -> bool {
| BytecodeType::TraitObject(..) => false,
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(..)
| BytecodeType::This => {
panic!("unexpected type param")
Expand Down Expand Up @@ -4720,6 +4727,7 @@ pub fn mode(vm: &VM, ty: BytecodeType) -> MachineMode {
}
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::Tuple(_)
| BytecodeType::TypeParam(_)
| BytecodeType::This
Expand Down Expand Up @@ -4747,6 +4755,7 @@ pub fn size(vm: &VM, ty: BytecodeType) -> i32 {
BytecodeType::Tuple(..) => get_concrete_tuple_bty(vm, &ty).size(),
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::This => {
unreachable!()
Expand Down Expand Up @@ -4786,6 +4795,7 @@ pub fn align(vm: &VM, ty: BytecodeType) -> i32 {
BytecodeType::Tuple(_) => get_concrete_tuple_bty(vm, &ty).align(),
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::This => {
unreachable!()
Expand Down
1 change: 1 addition & 0 deletions dora-runtime/src/compiler/lazy_compilation_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub fn iterate_roots<F>(

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::TypeParam(_)
| BytecodeType::Unit
| BytecodeType::This => {
Expand Down
1 change: 1 addition & 0 deletions dora-runtime/src/vm/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ fn compare_concrete_types(

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::Ptr
| BytecodeType::This => {
unreachable!()
Expand Down
1 change: 1 addition & 0 deletions dora-runtime/src/vm/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub fn ty_implements_trait(

BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. }
| BytecodeType::Ptr
| BytecodeType::This => unreachable!(),
}
Expand Down
Loading

0 comments on commit 2f301db

Please sign in to comment.