Explorer: basic abstract class support (#2441)

Relates to #1881 

Features:
* Add basic support for `abstract` class
    * Allow extending an abstract class
    * Prevent direct instantiation of an abstract class
Changes:
* Check that class extensibility for `VariableDefinition` is not `Abstract`
* Add corresponding set of lit tests
This commit is contained in:
Adrien Leravat
2022-12-06 09:35:55 -08:00
committed by GitHub
parent 94c207158e
commit 1d0bb85d0c
7 changed files with 107 additions and 9 deletions
+10 -2
View File
@@ -1777,6 +1777,15 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
}
case StatementKind::VariableDefinition: {
const auto& definition = cast<VariableDefinition>(stmt);
const auto* dest_type = &definition.pattern().static_type();
if (const auto* dest_class = dyn_cast<NominalClassType>(dest_type)) {
if (dest_class->declaration().extensibility() ==
ClassExtensibility::Abstract) {
return ProgramError(stmt.source_loc())
<< "Cannot instantiate abstract class "
<< dest_class->declaration().name();
}
}
if (act.pos() == 0 && definition.has_init()) {
// { {(var x = e) :: C, E, F} :: S, H}
// -> { {e :: (var x = []) :: C, E, F} :: S, H}
@@ -1790,8 +1799,7 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
Nonnull<const Value*> v;
if (definition.has_init()) {
CARBON_ASSIGN_OR_RETURN(
v, Convert(act.results()[0], &definition.pattern().static_type(),
stmt.source_loc()));
v, Convert(act.results()[0], dest_type, stmt.source_loc()));
} else {
v = arena_->New<UninitializedValue>(p);
}
-5
View File
@@ -4504,11 +4504,6 @@ auto TypeChecker::DeclareClassDeclaration(Nonnull<ClassDeclaration*> class_decl,
ImplScope class_scope;
class_scope.AddParent(scope_info.innermost_scope);
if (class_decl->extensibility() == ClassExtensibility::Abstract) {
return ProgramError(class_decl->source_loc())
<< "Class prefix `abstract` is not supported yet";
}
std::optional<Nonnull<const NominalClassType*>> base_class;
if (class_decl->base_expr().has_value()) {
Nonnull<Expression*> base_class_expr = *class_decl->base_expr();
+27
View File
@@ -0,0 +1,27 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: d.a=1
// CHECK:STDOUT: d.b=2
// CHECK:STDOUT: result: 0
package ExplorerTest api;
abstract class C {
var a: i32;
}
class D extends C {
var b: i32;
}
fn Main() -> i32 {
var d: D = { .base = {.a = 1}, .b = 2 };
Print("d.a={0}", d.a);
Print("d.b={0}", d.b);
return 0;
}
@@ -0,0 +1,25 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
abstract class C {
var a: i32;
}
class D extends C {
var b: i32;
}
fn Main() -> i32 {
var d: D = { .base = {.a = 1}, .b = 2 };
// Not supported yet.
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_abstract_class_subtyping.carbon:[[@LINE+1]]: type error in name binding: 'class D*' is not implicitly convertible to 'class C*'
var c: C* = &d;
return 0;
}
@@ -0,0 +1,23 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
abstract class C {
// TODO: Returning `Self` for an abstract class should error: `partial Self` should be used instead. This should be updated when `partial` is implemented.
fn Create() -> Self {
return { .a = 1 };
}
var a: i32;
}
fn Main() -> i32 {
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/testdata/class/fail_instantiate_abstract_class_constructor.carbon:[[@LINE+1]]: Cannot instantiate abstract class C
var c: C = C.Create();
return 0;
}
@@ -9,10 +9,11 @@
package ExplorerTest api;
abstract class C {
fn F() {}
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_abstract_class.carbon:[[@LINE+1]]: Class prefix `abstract` is not supported yet
var a: i32;
}
fn Main() -> i32 {
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/testdata/class/fail_instantiate_abstract_class_empty.carbon:[[@LINE+1]]: Cannot instantiate abstract class C
var c: C;
return 0;
}
@@ -0,0 +1,19 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// RUN: %{not} %{explorer-run}
// RUN: %{not} %{explorer-run-trace}
package ExplorerTest api;
abstract class C {
var a: i32;
}
fn Main() -> i32 {
// CHECK:STDERR: RUNTIME ERROR: {{.*}}/explorer/testdata/class/fail_instantiate_abstract_class_struct.carbon:[[@LINE+1]]: Cannot instantiate abstract class C
var c: C = { .a = 1 };
return 0;
}