mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-03 12:45:48 +01:00
Add braces for CARBON_KIND uses that lack them (#5882)
Also document why these are expected on `CARBON_KIND`. In
`kind_switch_test.cpp`, drop the `str` variable.
My recollection of the original discussion of `CARBON_KIND` is that it
should always have braces due to the risk of confusion for statement
interpretation, similar to a typical `if`/`else` but more subtle due to
the macro.
For example:
```
case CARBON_KIND(int n):
str << "int = " << n;
return str.TakeStr();
```
is equivalent to:
```
case CARBON_KIND(int n): {
str << "int = " << n;
}
return str.TakeStr();
```
This happens to work in context because `str` isn't scoped, but a
trivial refactoring to move `RawStringOstream str;` the first statement
of the `case` would probably have non-obvious results. For example:
```
case CARBON_KIND(int n):
RawStringOstream str; // Valid name shadowing, destructed without use.
str << "int = " << n; // Name lookup error on `n`.
return str.TakeStr();
```
This commit is contained in:
@@ -123,36 +123,46 @@ class StepStack {
|
||||
auto PushArray(llvm::ArrayRef<PushItem> items) -> void {
|
||||
for (auto item : llvm::reverse(items)) {
|
||||
CARBON_KIND_SWITCH(item) {
|
||||
case CARBON_KIND(InstId inst_id):
|
||||
case CARBON_KIND(InstId inst_id): {
|
||||
PushInstId(inst_id);
|
||||
break;
|
||||
case CARBON_KIND(llvm::StringRef string):
|
||||
}
|
||||
case CARBON_KIND(llvm::StringRef string): {
|
||||
PushString(string);
|
||||
break;
|
||||
case CARBON_KIND(NameId name_id):
|
||||
}
|
||||
case CARBON_KIND(NameId name_id): {
|
||||
PushNameId(name_id);
|
||||
break;
|
||||
case CARBON_KIND(ElementIndex element_index):
|
||||
}
|
||||
case CARBON_KIND(ElementIndex element_index): {
|
||||
PushElementIndex(element_index);
|
||||
break;
|
||||
case CARBON_KIND(QualifiedNameItem qualified_name):
|
||||
}
|
||||
case CARBON_KIND(QualifiedNameItem qualified_name): {
|
||||
PushQualifiedName(qualified_name.first, qualified_name.second);
|
||||
break;
|
||||
case CARBON_KIND(EntityNameItem entity_name):
|
||||
}
|
||||
case CARBON_KIND(EntityNameItem entity_name): {
|
||||
PushEntityName(entity_name.first, entity_name.second);
|
||||
break;
|
||||
case CARBON_KIND(EntityNameId entity_name_id):
|
||||
}
|
||||
case CARBON_KIND(EntityNameId entity_name_id): {
|
||||
PushEntityNameId(entity_name_id);
|
||||
break;
|
||||
case CARBON_KIND(TypeId type_id):
|
||||
}
|
||||
case CARBON_KIND(TypeId type_id): {
|
||||
PushTypeId(type_id);
|
||||
break;
|
||||
case CARBON_KIND(SpecificInterface specific_interface):
|
||||
}
|
||||
case CARBON_KIND(SpecificInterface specific_interface): {
|
||||
PushSpecificInterface(specific_interface);
|
||||
break;
|
||||
case CARBON_KIND(llvm::ListSeparator * sep):
|
||||
}
|
||||
case CARBON_KIND(llvm::ListSeparator * sep): {
|
||||
PushString(*sep);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user