From b03d0d542bf5b7f7c4eeaa4f77f27176e35da8cf Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Thu, 15 Jun 2023 08:32:29 -0700 Subject: [PATCH] Disallow calling Print() at compile time (#2903) Closes #2865 --- explorer/interpreter/interpreter.cpp | 4 ++++ .../print/fail_call_at_compile_time.carbon | 14 ++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 explorer/testdata/print/fail_call_at_compile_time.carbon diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 7b3552448897..a0e29830c725 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -1613,6 +1613,10 @@ auto Interpreter::StepExp() -> ErrorOr { const auto& args = cast(*act.results()[0]).elements(); switch (cast(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 format_string_value, Convert(args[0], arena_->New(), exp.source_loc())); diff --git a/explorer/testdata/print/fail_call_at_compile_time.carbon b/explorer/testdata/print/fail_call_at_compile_time.carbon new file mode 100644 index 000000000000..11c4399f4c1d --- /dev/null +++ b/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; }