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);
}