Skip to content

Commit

Permalink
Add and test new'ing an already-managed class type.
Browse files Browse the repository at this point in the history
Signed-off-by: Danila Fedorin <[email protected]>
  • Loading branch information
DanilaFe committed Feb 7, 2025
1 parent 3bbb87c commit 8650017
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions frontend/lib/resolution/Resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5402,6 +5402,13 @@ getDecoratedClassForNew(Context* context, const New* node,

switch (node->management()) {
case New::DEFAULT_MANAGEMENT:
// Management might've been provided for us already; otherwise
// fall back to the default: 'owned'.
if (!classType->decorator().isUnknownManagement())
break;

// Fall through to 'owned' management.

case New::OWNED:
decorator = ClassTypeDecorator(ClassTypeDecorator::MANAGED);
manager = AnyOwnedType::get(context);
Expand Down
18 changes: 18 additions & 0 deletions frontend/test/resolution/testNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,23 @@ static void testUserGenericNew() {
assert(guard.realizeErrors() == 1);
}

static void testExplicitManagementNew() {
auto ctx = buildStdContext();
ErrorGuard guard(ctx);

auto var = resolveTypeOfXInit(ctx,
R"""(
class C{}
proc getType() type do return unmanaged C;
var x = new (getType())();
)""");

assert(var.type()->isClassType());
assert(var.type()->toClassType()->decorator().isUnmanaged());
}


int main() {
testEmptyRecordUserInit();
Expand All @@ -1056,6 +1073,7 @@ int main() {
testCompilerGeneratedGenericNewClass();
testSimpleUserGenericNew();
testUserGenericNew();
testExplicitManagementNew();

return 0;
}
Expand Down

0 comments on commit 8650017

Please sign in to comment.