Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow creating instances of abstract classes #4381

Merged
merged 21 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions toolchain/check/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,9 @@ auto Context::TryToCompleteType(SemIR::TypeId type_id,
}

auto builder = abstract_diagnoser();
if (!builder) {
return false;
}
NoteAbstractClass(class_type->class_id, builder);
builder.Emit();
return false;
Expand Down
9 changes: 4 additions & 5 deletions toolchain/check/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,9 @@ class Context {
// If the type is not complete, `diagnoser` is invoked to diagnose the issue,
// if a `diagnoser` is provided. The builder it returns will be annotated to
// describe the reason why the type is not complete.
auto TryToCompleteType(
SemIR::TypeId type_id,
BuildDiagnosticFn diagnoser = nullptr,
BuildDiagnosticFn abstract_diagnoser = nullptr)
auto TryToCompleteType(SemIR::TypeId type_id,
BuildDiagnosticFn diagnoser = nullptr,
BuildDiagnosticFn abstract_diagnoser = nullptr)
-> bool;

// Attempts to complete and define the type `type_id`. Returns `true` if the
Expand All @@ -341,7 +340,7 @@ class Context {
// type error and returns an error type. This is a convenience wrapper around
// TryToCompleteType. `diagnoser` must not be null.
auto AsCompleteType(SemIR::TypeId type_id, BuildDiagnosticFn diagnoser,
BuildDiagnosticFn abstract_diagnoser)
BuildDiagnosticFn abstract_diagnoser = nullptr)
-> SemIR::TypeId {
return TryToCompleteType(type_id, diagnoser, abstract_diagnoser)
? type_id
Expand Down
31 changes: 9 additions & 22 deletions toolchain/check/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,7 @@ static auto ConvertStructToClass(Context& context, SemIR::StructType src_type,
ConversionTarget target) -> SemIR::InstId {
PendingBlock target_block(context);
auto& dest_class_info = context.classes().Get(dest_type.class_id);
if (dest_class_info.inheritance_kind == SemIR::Class::Abstract) {
CARBON_DIAGNOSTIC(ConstructionOfAbstractClass, Error,
"cannot construct instance of abstract class; "
"consider using `partial {0}` instead",
SemIR::TypeId);
context.emitter().Emit(value_id, ConstructionOfAbstractClass,
target.type_id);
return SemIR::InstId::BuiltinError;
}
CARBON_CHECK(dest_class_info.inheritance_kind != SemIR::Class::Abstract);
auto object_repr_id =
dest_class_info.GetObjectRepr(context.sem_ir(), dest_type.specific_id);
if (object_repr_id == SemIR::TypeId::Error) {
Expand Down Expand Up @@ -963,19 +955,14 @@ auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
CARBON_DIAGNOSTIC(AbstractTypeInInit, Error,
"initialization of abstract type `{0}`",
SemIR::TypeId);
CARBON_DIAGNOSTIC(AbstractTypeInValueConversion, Error,
"forming value of abstract type `{0}`",
SemIR::TypeId);
CARBON_DIAGNOSTIC(AbstractTypeInConversion, Error,
"invalid use of abstract type `{0}`",
SemIR::TypeId);
return context.emitter().Build(
loc_id,
target.is_initializer() ? AbstractTypeInInit
: target.kind == ConversionTarget::Value
? AbstractTypeInValueConversion
: AbstractTypeInConversion,
target.type_id);
if (!target.is_initializer()) {
return DiagnosticEmitter<SemIRLoc>::DiagnosticBuilder();
}
// return // add support for creating a null DiagnosticBuilder and
// return one here. Include null-testability and use that
// to skip adding notes in the caller if it's null.
dwblaikie marked this conversation as resolved.
Show resolved Hide resolved
return context.emitter().Build(loc_id, AbstractTypeInInit,
target.type_id);
})) {
return SemIR::InstId::BuiltinError;
}
Expand Down
31 changes: 11 additions & 20 deletions toolchain/check/handle_binding_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
llvm::StringLiteral, SemIR::TypeId);
return context.emitter().Build(
type_node, IncompleteTypeInVarDecl,
parent_class_decl ? llvm::StringLiteral("Field")
: llvm::StringLiteral("Variable"),
parent_class_decl ? llvm::StringLiteral("field")
: llvm::StringLiteral("variable"),
cast_type_id);
},
[&] {
Expand All @@ -117,8 +117,8 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
llvm::StringLiteral, SemIR::TypeId);
return context.emitter().Build(
type_node, AbstractTypeInVarDecl,
parent_class_decl ? llvm::StringLiteral("Field")
: llvm::StringLiteral("Variable"),
parent_class_decl ? llvm::StringLiteral("field")
: llvm::StringLiteral("variable"),
cast_type_id);
});
if (parent_class_decl) {
Expand Down Expand Up @@ -197,22 +197,13 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
}

case Parse::NodeKind::LetIntroducer: {
cast_type_id = context.AsCompleteType(
cast_type_id,
[&] {
CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
"`let` binding has incomplete type `{0}`",
SemIR::TypeId);
return context.emitter().Build(type_node, IncompleteTypeInLetDecl,
cast_type_id);
},
[&] {
CARBON_DIAGNOSTIC(AbstractTypeInLetDecl, Error,
"`let` binding has abstract type `{0}`",
SemIR::TypeId);
return context.emitter().Build(type_node, AbstractTypeInLetDecl,
cast_type_id);
});
cast_type_id = context.AsCompleteType(cast_type_id, [&] {
CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
"`let` binding has incomplete type `{0}`",
SemIR::TypeId);
return context.emitter().Build(type_node, IncompleteTypeInLetDecl,
cast_type_id);
});
// Create the instruction, but don't add it to a block until after we've
// formed its initializer.
// TODO: For general pattern parsing, we'll need to create a block to hold
Expand Down
15 changes: 6 additions & 9 deletions toolchain/check/handle_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,12 @@ static auto DiagnoseBaseIsFinal(Context& context, Parse::NodeId node_id,
static auto CheckBaseType(Context& context, Parse::NodeId node_id,
SemIR::InstId base_expr_id) -> BaseInfo {
auto base_type_id = ExprAsType(context, node_id, base_expr_id).type_id;
base_type_id = context.AsCompleteType(
base_type_id,
[&] {
CARBON_DIAGNOSTIC(IncompleteTypeInBaseDecl, Error,
"base `{0}` is an incomplete type", SemIR::TypeId);
return context.emitter().Build(node_id, IncompleteTypeInBaseDecl,
base_type_id);
},
nullptr);
base_type_id = context.AsCompleteType(base_type_id, [&] {
CARBON_DIAGNOSTIC(IncompleteTypeInBaseDecl, Error,
"base `{0}` is an incomplete type", SemIR::TypeId);
return context.emitter().Build(node_id, IncompleteTypeInBaseDecl,
base_type_id);
});

if (base_type_id == SemIR::TypeId::Error) {
return BaseInfo::Error;
Expand Down
29 changes: 9 additions & 20 deletions toolchain/check/handle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,26 +364,15 @@ static auto HandleFunctionDefinitionAfterSignature(
SemIR::Function::GetParamFromParamRefId(context.sem_ir(), param_ref_id);

// The parameter types need to be complete.
context.TryToCompleteType(
param_info.inst.type_id,
[&] {
CARBON_DIAGNOSTIC(
IncompleteTypeInFunctionParam, Error,
"parameter has incomplete type `{0}` in function definition",
SemIR::TypeId);
return context.emitter().Build(param_info.inst_id,
IncompleteTypeInFunctionParam,
param_info.inst.type_id);
},
[&] {
CARBON_DIAGNOSTIC(
AbstractTypeInFunctionParam, Error,
"parameter has abstract type `{0}` in function definition",
SemIR::TypeId);
return context.emitter().Build(param_info.inst_id,
AbstractTypeInFunctionParam,
param_info.inst.type_id);
});
context.TryToCompleteType(param_info.inst.type_id, [&] {
CARBON_DIAGNOSTIC(
IncompleteTypeInFunctionParam, Error,
"parameter has incomplete type `{0}` in function definition",
SemIR::TypeId);
return context.emitter().Build(param_info.inst_id,
IncompleteTypeInFunctionParam,
param_info.inst.type_id);
});
}

context.node_stack().Push(node_id, function_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Incomplete;

// CHECK:STDERR: fail_incomplete_element.carbon:[[@LINE+6]]:8: error: Variable has incomplete type `[Incomplete; 1]`
// CHECK:STDERR: fail_incomplete_element.carbon:[[@LINE+6]]:8: error: variable has incomplete type `[Incomplete; 1]`
// CHECK:STDERR: var a: [Incomplete; 1];
// CHECK:STDERR: ^~~~~~~~~~~~~~~
// CHECK:STDERR: fail_incomplete_element.carbon:[[@LINE-5]]:1: note: class was forward declared here
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/testdata/class/cross_package_import.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ library "[[@TEST_NAME]]";

import Other library "other_extern";

// CHECK:STDERR: fail_extern.carbon:[[@LINE+8]]:8: error: Variable has incomplete type `C`
// CHECK:STDERR: fail_extern.carbon:[[@LINE+8]]:8: error: variable has incomplete type `C`
// CHECK:STDERR: var c: Other.C = {};
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_extern.carbon:[[@LINE-5]]:1: in import
Expand Down
Loading