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

Disallow calling Print() at compile time (#2903)

Closes #2865
Geoff Romer 2 лет назад
Родитель
Сommit
b03d0d542b

+ 4 - 0
explorer/interpreter/interpreter.cpp

@@ -1613,6 +1613,10 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
       const auto& args = cast<TupleValue>(*act.results()[0]).elements();
       switch (cast<IntrinsicExpression>(exp).intrinsic()) {
         case IntrinsicExpression::Intrinsic::Print: {
+          if (phase_ != Phase::RunTime) {
+            return ProgramError(exp.source_loc())
+                   << "Print called before run time";
+          }
           CARBON_ASSIGN_OR_RETURN(
               Nonnull<const Value*> format_string_value,
               Convert(args[0], arena_->New<StringType>(), exp.source_loc()));

+ 14 - 0
explorer/testdata/print/fail_call_at_compile_time.carbon

@@ -0,0 +1,14 @@
+// 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
+
+package ExplorerTest impl;
+
+interface I {}
+
+// CHECK:STDERR: COMPILATION ERROR: fail_call_at_compile_time.carbon:[[@LINE+1]]: Print called before run time
+impl forall [T:! String] Print(T) as I {}
+
+fn Main() -> i32 { return 0; }