Explorer: Report compiletime error when global variable type is abstract (#2748)

Add concrete type check for Variable Declaration to report a compile time error when the variable type is abstract

Closes #2747
This commit is contained in:
Amr Hesham
2023-04-07 17:37:08 -07:00
committed by GitHub
parent 0177764a03
commit 0dc80bac01
2 changed files with 36 additions and 0 deletions
+17
View File
@@ -309,6 +309,21 @@ static auto ExpectCompleteType(SourceLocation source_loc,
<< "incomplete type `" << *type << "` used in " << context;
}
// Expect that a type is concrete. Issue a diagnostic if not.
static auto ExpectConcreteType(SourceLocation source_loc,
Nonnull<const Value*> type) -> ErrorOr<Success> {
CARBON_CHECK(IsType(type));
if (const auto* dest_class = dyn_cast<NominalClassType>(type)) {
if (dest_class->declaration().extensibility() ==
ClassExtensibility::Abstract) {
return ProgramError(source_loc) << "Cannot instantiate abstract class "
<< dest_class->declaration().name();
}
}
return Success();
}
// Returns whether *value represents the type of a Carbon value, as
// opposed to a type pattern or a non-type value.
static auto TypeContainsAuto(Nonnull<const Value*> type) -> bool {
@@ -6134,6 +6149,8 @@ auto TypeChecker::DeclareDeclaration(Nonnull<Declaration*> d,
var.expression_category()));
CARBON_RETURN_IF_ERROR(ExpectCompleteType(
var.source_loc(), "type of variable", &var.binding().static_type()));
CARBON_RETURN_IF_ERROR(
ExpectConcreteType(var.source_loc(), &var.binding().static_type()));
var.set_static_type(&var.binding().static_type());
break;
}
@@ -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 B {
}
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/global_variable/fail_instantiate_global_abstract.carbon:[[@LINE+1]]: Cannot instantiate abstract class B
var b: B = {};
fn Main() -> i32 {
return 0;
}