Browse Source

Fix for a crash caused by choice alternative name being the same as choice name (#1243)

Fixes #1248

Changed resolve names logic to add choice name to name resolution after choice's alternatives are added.
pk19604014 4 years ago
parent
commit
589930b703

+ 26 - 0
explorer/fuzzing/fuzzer_corpus/dedba2ac81916ee306624a2ac3db2a3c7a4ea5ad.textproto

@@ -0,0 +1,26 @@
+compilation_unit {
+  declarations {
+    choice {
+      alternatives {
+        signature {
+          primitive_operator {
+            arguments {
+              intrinsic {
+                argument {
+                  fields {
+                    primitive_operator {
+                      arguments {
+                        identifier {
+                        }
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}

+ 3 - 2
explorer/interpreter/resolve_names.cpp

@@ -47,8 +47,8 @@ static auto AddExposedNames(const Declaration& declaration,
       break;
     }
     case DeclarationKind::ChoiceDeclaration: {
-      auto& choice = cast<ChoiceDeclaration>(declaration);
-      CARBON_RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice));
+      // Choice name is added to the scope after the choice's alternatives.
+      // See https://github.com/carbon-language/carbon-lang/issues/1248.
       break;
     }
     case DeclarationKind::VariableDeclaration: {
@@ -424,6 +424,7 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
                  << "` in choice type";
         }
       }
+      CARBON_RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice));
       break;
     }
     case DeclarationKind::VariableDeclaration: {