Просмотр исходного кода

Validate that the argument to __mix is a mixin. (#2757)

Jon Ross-Perkins 3 лет назад
Родитель
Сommit
9245e17d4c

+ 6 - 1
explorer/interpreter/type_checker.cpp

@@ -6129,7 +6129,12 @@ auto TypeChecker::DeclareDeclaration(Nonnull<Declaration*> d,
       CARBON_ASSIGN_OR_RETURN(
           Nonnull<const Value*> mixin,
           InterpExp(&mix_decl.mixin(), arena_, trace_stream_));
-      mix_decl.set_mixin_value(cast<MixinPseudoType>(mixin));
+      if (const auto* mixin_value = dyn_cast<MixinPseudoType>(mixin)) {
+        mix_decl.set_mixin_value(mixin_value);
+      } else {
+        return ProgramError(mix_decl.source_loc())
+               << "Not a valid mixin: `" << mix_decl.mixin() << "`";
+      }
       const auto& mixin_decl = mix_decl.mixin_value().declaration();
       if (!mixin_decl.is_declared()) {
         return ProgramError(mix_decl.source_loc())

+ 18 - 0
explorer/testdata/mixin/fail_mix_invalid.carbon

@@ -0,0 +1,18 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+// AUTOUPDATE
+// RUN: %{not} %{explorer-run}
+// RUN: %{not} %{explorer-run-trace}
+
+package ExplorerTest api;
+
+__mixin Mixin {
+  // CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/mixin/fail_mix_invalid.carbon:[[@LINE+1]]: Not a valid mixin: `()`
+  __mix ();
+}
+
+fn Main() -> i32 {
+  return 0;
+}