Skip to content

Commit

Permalink
fix generic struct-like enums options
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugobros3 committed Jul 9, 2024
1 parent 2263837 commit 5d1e2b6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 1 addition & 3 deletions include/artic/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,7 @@ struct TypeApp : public Type {
}

/// Returns the type of the given member of the applied type, if it is a complex type.
const Type* member_type(size_t i) const {
return applied->as<ComplexType>()->member_type(i)->replace(replace_map());
}
const Type* member_type(size_t i) const;

void print(Printer&) const override;
bool equals(const Type*) const override;
Expand Down
2 changes: 2 additions & 0 deletions src/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ const artic::Type* Path::infer(TypeChecker& checker, bool value_expected, Ptr<Ex
if (enum_type->decl.options[*index]->struct_type) {
// If the enumeration option uses the record syntax, we use the corresponding structure type
type = enum_type->decl.options[*index]->struct_type;
if (type_app)
type = checker.type_table.type_app(type->as<StructType>(), type_app->type_args);
is_value = false;
is_ctor = true;
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ const ModType::Members& ModType::members() const {
return *members_;
}

const Type* TypeApp::member_type(size_t i) const {
if (auto enum_t = applied->isa<EnumType>(); enum_t && enum_t->decl.options[i]->struct_type)
return type_table.type_app(enum_t->decl.options[i]->struct_type->as<StructType>(), type_args);
return applied->as<ComplexType>()->member_type(i)->replace(replace_map());
}

// Misc. ---------------------------------------------------------------------------

bool Type::subtype(const Type* other) const {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_test(NAME simple_enums1 COMMAND artic --print-ast ${CMAKE_CURRENT_SOURC
add_test(NAME simple_enums2 COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/enums2.art)
add_test(NAME simple_enums3 COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/enums3.art)
add_test(NAME simple_enums4 COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/enums4.art)
add_test(NAME simple_enums5 COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/enums5.art)
add_test(NAME simple_types COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/types.art)
add_test(NAME simple_loops COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/loops.art)
add_test(NAME simple_return COMMAND artic --print-ast ${CMAKE_CURRENT_SOURCE_DIR}/simple/return.art)
Expand Down
14 changes: 14 additions & 0 deletions test/simple/enums5.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
enum G[T] { A, B(bool), C { x: bool, y: T }, D {} }
#[export]
fn woah(k: G[i32]) {
match k {
G[i32]::A => 1,
G[i32]::B(true) => 3,
G[i32]::B(false) => 4,
G[i32]::C { x = true, y = 1 } => 5,
G[i32]::C { x = true, y = _ } => 6,
G[i32]::C { x = false, y = 2 } => 7,
G[i32]::C { x = false, y = _ } => 8,
G[i32]::D {} => 9
}
}

0 comments on commit 5d1e2b6

Please sign in to comment.