Skip to content

Commit

Permalink
[error] String reference field initialized with new string storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Sep 21, 2024
1 parent f7475f0 commit ef7574c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sema.fu
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@ public class FuSema
if (field.Value != null) {
field.Value = VisitExpr(field.Value);
if (!field.IsAssignableStorage())
Coerce(field.Value, field.Type is FuArrayStorageType array ? array.GetElementType() : field.Type);
CoercePermanent(field.Value, field.Type is FuArrayStorageType array ? array.GetElementType() : field.Type);
}
break;
case FuMethod! method:
Expand Down
2 changes: 1 addition & 1 deletion libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6780,7 +6780,7 @@ void FuSema::resolveCode(FuClass * klass)
field->value = visitExpr(field->value);
if (!field->isAssignableStorage()) {
const FuArrayStorageType * array;
coerce(field->value.get(), (array = dynamic_cast<const FuArrayStorageType *>(field->type.get())) ? array->getElementType().get() : field->type.get());
coercePermanent(field->value.get(), (array = dynamic_cast<const FuArrayStorageType *>(field->type.get())) ? array->getElementType().get() : field->type.get());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6876,7 +6876,7 @@ void ResolveCode(FuClass klass)
if (field.Value != null) {
field.Value = VisitExpr(field.Value);
if (!field.IsAssignableStorage())
Coerce(field.Value, field.Type is FuArrayStorageType array ? array.GetElementType() : field.Type);
CoercePermanent(field.Value, field.Type is FuArrayStorageType array ? array.GetElementType() : field.Type);
}
break;
case FuMethod method:
Expand Down
2 changes: 1 addition & 1 deletion libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -7262,7 +7262,7 @@ export class FuSema
field.value = this.#visitExpr(field.value);
if (!field.isAssignableStorage()) {
let array;
this.#coerce(field.value, (array = field.type) instanceof FuArrayStorageType ? array.getElementType() : field.type);
this.#coercePermanent(field.value, (array = field.type) instanceof FuArrayStorageType ? array.getElementType() : field.type);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion test/ObjectFieldInit.fu
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ public class Test
{
int Foo = 42;

static int Init() => 5;

int Bar = Init();

public static bool Run()
{
Test() o;
return o.Foo == 42;
return o.Foo == 42 && o.Bar == 5;
}
}
2 changes: 2 additions & 0 deletions test/error/StringLeak.fu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static class Test

public static string GetValue(Match match) => match.Value; //ERROR: New string must be assigned to 'string()'

string Field = "FOO".ToLower(); //ERROR: New string must be assigned to 'string()'

public static bool Run()
{
return true;
Expand Down

0 comments on commit ef7574c

Please sign in to comment.