mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-29 13:14:57 +01:00
Basic support for incomplete types. (#3302)
Incomplete types may be nested within other types; for example, a tuple type might have an incomplete type as an element. Handle such cases by walking through nested incomplete types when completing a type. This is done non-recursively in case a very complex type is formed. Types are generally no longer completed at the point where they're formed. Instead, we attempt to complete a type when it is used in a context that requires a complete type, and diagnose if the type cannot be completed at that point. This will be necessary for classes, which can become complete after their first use, and helps tease out bugs where a type completeness check is missing.
This commit is contained in:
@@ -20,6 +20,7 @@ auto HandleGenericPatternBinding(Context& context, Parse::Node parse_node)
|
||||
auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
|
||||
auto [type_node, parsed_type_id] =
|
||||
context.node_stack().PopExpressionWithParseNode();
|
||||
auto type_node_copy = type_node;
|
||||
auto cast_type_id = ExpressionAsType(context, type_node, parsed_type_id);
|
||||
|
||||
// Get the name.
|
||||
@@ -32,14 +33,38 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
|
||||
switch (auto context_parse_node_kind = context.parse_tree().node_kind(
|
||||
context.node_stack().PeekParseNode())) {
|
||||
case Parse::NodeKind::VariableIntroducer:
|
||||
if (!context.TryToCompleteType(cast_type_id, [&] {
|
||||
CARBON_DIAGNOSTIC(IncompleteTypeInVarDeclaration, Error,
|
||||
"Variable has incomplete type `{0}`.",
|
||||
std::string);
|
||||
return context.emitter().Build(
|
||||
type_node_copy, IncompleteTypeInVarDeclaration,
|
||||
context.semantics_ir().StringifyType(cast_type_id, true));
|
||||
})) {
|
||||
cast_type_id = SemIR::TypeId::Error;
|
||||
}
|
||||
context.AddNodeAndPush(
|
||||
parse_node, SemIR::VarStorage(name_node, cast_type_id, name_id));
|
||||
break;
|
||||
|
||||
case Parse::NodeKind::ParameterListStart:
|
||||
// Parameters can have incomplete types in a function declaration, but not
|
||||
// in a function definition. We don't know which kind we have here.
|
||||
context.AddNodeAndPush(
|
||||
parse_node, SemIR::Parameter(name_node, cast_type_id, name_id));
|
||||
break;
|
||||
|
||||
case Parse::NodeKind::LetIntroducer:
|
||||
if (!context.TryToCompleteType(cast_type_id, [&] {
|
||||
CARBON_DIAGNOSTIC(IncompleteTypeInLetDeclaration, Error,
|
||||
"`let` binding has incomplete type `{0}`.",
|
||||
std::string);
|
||||
return context.emitter().Build(
|
||||
type_node_copy, IncompleteTypeInLetDeclaration,
|
||||
context.semantics_ir().StringifyType(cast_type_id, true));
|
||||
})) {
|
||||
cast_type_id = SemIR::TypeId::Error;
|
||||
}
|
||||
// Create the node, 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
|
||||
@@ -49,6 +74,7 @@ auto HandlePatternBinding(Context& context, Parse::Node parse_node) -> bool {
|
||||
context.semantics_ir().AddNodeInNoBlock(SemIR::BindName(
|
||||
name_node, cast_type_id, name_id, SemIR::NodeId::Invalid)));
|
||||
break;
|
||||
|
||||
default:
|
||||
CARBON_FATAL() << "Found a pattern binding in unexpected context "
|
||||
<< context_parse_node_kind;
|
||||
|
||||
Reference in New Issue
Block a user