Disallow creating instances of abstract classes (#4381)

A good first-pass, at least. (abstract adapters are rejected with this
change, though pending further language design discussion)

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
David Blaikie
2024-10-12 15:17:42 +00:00
committed by GitHub
co-authored by Richard Smith
parent bafddd8711
commit d491387a98
15 changed files with 870 additions and 145 deletions
+22 -10
View File
@@ -100,16 +100,28 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
// A `var` declaration at class scope introduces a field.
auto parent_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
cast_type_id = context.AsCompleteType(cast_type_id, [&] {
CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
"{0} has incomplete type {1}", llvm::StringLiteral,
InstIdAsType);
return context.emitter().Build(type_node, IncompleteTypeInVarDecl,
parent_class_decl
? llvm::StringLiteral("Field")
: llvm::StringLiteral("Variable"),
cast_type_inst_id);
});
cast_type_id = context.AsCompleteType(
cast_type_id,
[&] {
CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
"{0} has incomplete type {1}",
llvm::StringLiteral, SemIR::TypeId);
return context.emitter().Build(
type_node, IncompleteTypeInVarDecl,
parent_class_decl ? llvm::StringLiteral("field")
: llvm::StringLiteral("variable"),
cast_type_id);
},
[&] {
CARBON_DIAGNOSTIC(AbstractTypeInVarDecl, Error,
"{0} has abstract type {1}", llvm::StringLiteral,
SemIR::TypeId);
return context.emitter().Build(
type_node, AbstractTypeInVarDecl,
parent_class_decl ? llvm::StringLiteral("field")
: llvm::StringLiteral("variable"),
cast_type_id);
});
if (parent_class_decl) {
CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer,
"`returned var` at class scope");