Skip to content

Commit

Permalink
frontend: Require named arguments for struct ctors as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Oct 31, 2024
1 parent 5b4479a commit b7e9153
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 31 deletions.
4 changes: 2 additions & 2 deletions dora-frontend/src/generator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2436,7 +2436,7 @@ fn gen_new_struct() {
gen_fct(
"
struct Foo { f1: Int32, f2: Bool }
fn f(): Foo { Foo(10i32, false) }
fn f(): Foo { Foo(f1 = 10i32, f2 = false) }
",
|sa, code, fct| {
let struct_id = struct_by_name(sa, "Foo");
Expand All @@ -2463,7 +2463,7 @@ fn gen_new_struct() {
gen_fct(
"
struct Foo[T] { f1: T, f2: Bool }
fn f[T](val: T): Foo[T] { Foo[T](val, false) }
fn f[T](val: T): Foo[T] { Foo[T](f1 = val, f2 = false) }
",
|sa, code, fct| {
let struct_id = struct_by_name(sa, "Foo");
Expand Down
10 changes: 0 additions & 10 deletions dora-frontend/src/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,6 @@ impl CallArguments {
self.positional_types(ck)
}

fn all_named(&self) -> bool {
for arg in &self.arguments {
if arg.name.is_none() && !arg.expr.is_ident() {
return false;
}
}

true
}

fn positional_types(&self, ck: &TypeCheck) -> Vec<SourceType> {
self.arguments
.iter()
Expand Down
10 changes: 1 addition & 9 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,9 @@ fn check_expr_call_struct(
return ty_error();
}

if struct_.requires_named_arguments() && arguments.all_named() {
if struct_.requires_named_arguments() {
check_expr_call_ctor_with_named_fields(ck, struct_, type_params.clone(), &arguments);
} else {
if struct_.requires_named_arguments() && arguments.arguments.len() > 1 {
ck.sa.warn(
ck.file_id,
arguments.span,
ErrorMessage::CallRequiresNamedArgument,
);
}

check_expr_call_ctor_with_unnamed_fields(ck, struct_, type_params.clone(), &arguments);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/array/array-store-struct1.dora
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
std::forceCollect();

let new_bar = Bar(2100);
vec.push(Foo(10i32, new_bar, old_bar));
vec.push(Foo(value = 10i32, bar1 = new_bar, bar2 = old_bar));
}

struct Foo {
Expand Down
2 changes: 1 addition & 1 deletion tests/cannon/gc-in-compiler1.dora
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ fn test3(arg1: Int64, arg2: Int64, arg3: Int64, arg4: Int64, arg5: Int64, arg6:
}

fn test4(f: Foo): MyPair {
MyPair(f.x, f.y)
MyPair(x = f.x, y = f.y)
}
2 changes: 1 addition & 1 deletion tests/enum/enum-tagged2.dora
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Data {
enum Foo { A(Int32, String, Data, (Int32, Int32), Float64), B }

fn main() {
let value = Foo::A(112i32, "abc", Data(12i32, 47i32, "def"), (3i32, 4i32), 105.5);
let value = Foo::A(112i32, "abc", Data(x = 12i32, y = 47i32, z = "def"), (3i32, 4i32), 105.5);
match value {
Foo::A(a, b, c, d, e) => {
assert(a == 112i32);
Expand Down
2 changes: 1 addition & 1 deletion tests/language/global7.dora
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Foo {
c: Int64,
}

let foo: Foo = Foo(100, 200, 300);
let foo: Foo = Foo(a = 100, b = 200, c = 300);

fn main() {
let tmp = foo;
Expand Down
4 changes: 2 additions & 2 deletions tests/language/global8.dora
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ struct Foo {
c: Int64,
}

let mut foo: Foo = Foo(100, 200, 300);
let mut foo: Foo = Foo(a = 100, b = 200, c = 300);

fn main() {
foo = Foo(300, 400, 500);
foo = Foo(a = 300, b = 400, c = 500);
assert(foo.a == 300);
assert(foo.b == 400);
assert(foo.c == 500);
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/hashmap-clone1.dora
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct Foo {

fn main() {
let map = HashMap[Int, Foo]::new();
map.insert(1, Foo("foo", "bar"));
map.insert(1, Foo(x = "foo", y = "bar"));
// This used to crash because of Array::copy observing null. However, this is
// fine because HashMap uses Array::unsafeNew internally and does not expose null.
let map = map.clone();
Expand Down
4 changes: 2 additions & 2 deletions tests/struct/struct-array5.dora
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ struct Bar {
}

fn main() {
let x = Array[Bar]::fill(256, Bar(0, Foo(1_000)));
let x = Array[Bar]::fill(256, Bar(integer = 0, foo = Foo(1_000)));
std::forceCollect();
x(255) = Bar(12, Foo(2_000));
x(255) = Bar(integer = 12, foo = Foo(2_000));
std::forceMinorCollect();
assert(x(255).integer == 12);
assert(x(255).foo.value == 2_000);
Expand Down
2 changes: 1 addition & 1 deletion tests/vec/vec-struct1.dora
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() {
}

fn build_vec(): Vec[Foo] {
Vec[Foo]::new(Foo(1i32, "a" + "b" + "c"), Foo(2i32, "d" + "e" + "f"), Foo(3i32, "g" + "h" + "i"))
Vec[Foo]::new(Foo(a = 1i32, b = "a" + "b" + "c"), Foo(a = 2i32, b = "d" + "e" + "f"), Foo(a = 3i32, b = "g" + "h" + "i"))
}

fn pop_vec(vec: Vec[Foo], expected: String) {
Expand Down

0 comments on commit b7e9153

Please sign in to comment.