From 6cf2272bbef49e980d3f6881398bb78aa711522b Mon Sep 17 00:00:00 2001 From: pmqtt <51272730+pmqtt@users.noreply.github.com> Date: Thu, 13 Oct 2022 19:44:53 +0200 Subject: [PATCH] Fix: Issue #1394 (#2266) * start correct * Fix #1394 * remove llvm::outs() * Fix #1394 * --- * fix issue-1394 * fix issue-1394 * Update explorer/interpreter/interpreter.cpp Co-authored-by: Geoff Romer * fix issue-1394 * remove and rename test files * add changes from j.m Co-authored-by: m new Co-authored-by: Geoff Romer --- explorer/interpreter/interpreter.cpp | 4 + .../assoc_const/fail_unknown_value.carbon | 25 ------ ...known_value_specified_in_constraint.carbon | 26 ------- .../auto_return/modify_return_type.carbon | 18 ----- .../testdata/function/auto_return/type.carbon | 19 ----- ....carbon => fail_param_type_is_call.carbon} | 6 +- .../function/fail_return_type_is_call.carbon | 20 +++++ .../function/fail_var_type_is_call.carbon | 18 +++++ ...carbon => fail_use_at_compile_time.carbon} | 6 +- .../instantiation_at_compile_time.carbon | 22 ------ ...arbon => fail_call_at_compile_time.carbon} | 6 +- .../evaluate_type_before_dot.carbon | 76 ------------------- 12 files changed, 51 insertions(+), 195 deletions(-) delete mode 100644 explorer/testdata/assoc_const/fail_unknown_value.carbon delete mode 100644 explorer/testdata/assoc_const/fail_unknown_value_specified_in_constraint.carbon delete mode 100644 explorer/testdata/function/auto_return/modify_return_type.carbon delete mode 100644 explorer/testdata/function/auto_return/type.carbon rename explorer/testdata/function/{auto_return/modify_arg_type.carbon => fail_param_type_is_call.carbon} (60%) create mode 100644 explorer/testdata/function/fail_return_type_is_call.carbon create mode 100644 explorer/testdata/function/fail_var_type_is_call.carbon rename explorer/testdata/generic_class/{use_at_compile_time.carbon => fail_use_at_compile_time.carbon} (63%) delete mode 100644 explorer/testdata/generic_class/instantiation_at_compile_time.carbon rename explorer/testdata/generic_function/{call_at_compile_time.carbon => fail_call_at_compile_time.carbon} (58%) delete mode 100644 explorer/testdata/member_access/evaluate_type_before_dot.carbon diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 2f18230cb003..6e81e368478f 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/explorer/interpreter/interpreter.cpp @@ -799,6 +799,10 @@ auto Interpreter::CallFunction(const CallExpression& call, alt.alt_name(), alt.choice_name(), arg)); } case Value::Kind::FunctionValue: { + if (phase() == Phase::CompileTime) { + return ProgramError(call.source_loc()) + << "Function would be called at compile-time"; + } const FunctionValue& fun_val = cast(*fun); const FunctionDeclaration& function = fun_val.declaration(); RuntimeScope binding_scope(&heap_); diff --git a/explorer/testdata/assoc_const/fail_unknown_value.carbon b/explorer/testdata/assoc_const/fail_unknown_value.carbon deleted file mode 100644 index 5e3ca25591dd..000000000000 --- a/explorer/testdata/assoc_const/fail_unknown_value.carbon +++ /dev/null @@ -1,25 +0,0 @@ -// 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 -// -// RUN: %{not} %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{not} %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s - -package ExplorerTest api; - -interface Iface { let N:! i32; } - -fn PickType(N: i32) -> Type { return i32; } - -fn F[T:! Iface](x: T) -> i32 { - // CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_unknown_value.carbon:[[@LINE+1]]: value of associated constant (T).N is not known - var x: PickType(T.N) = 0; - return x; -} - -impl i32 as Iface where .N == 5 {} - -fn Main() -> i32 { - return F(0); -} diff --git a/explorer/testdata/assoc_const/fail_unknown_value_specified_in_constraint.carbon b/explorer/testdata/assoc_const/fail_unknown_value_specified_in_constraint.carbon deleted file mode 100644 index 71262e2cd51d..000000000000 --- a/explorer/testdata/assoc_const/fail_unknown_value_specified_in_constraint.carbon +++ /dev/null @@ -1,26 +0,0 @@ -// 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 -// -// RUN: %{not} %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{not} %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s - -package ExplorerTest api; - -interface Iface { let N:! i32; } - -fn PickType(N: i32) -> Type { return i32; } - -fn F[T:! Iface where .N == 5](x: T) -> i32 { - // TODO: This should be valid: the value of T.N is known to be 5 here. - // CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_unknown_value_specified_in_constraint.carbon:[[@LINE+1]]: value of associated constant (T).N is not known - var x: PickType(T.N) = 0; - return x; -} - -impl i32 as Iface where .N == 5 {} - -fn Main() -> i32 { - return F(0); -} diff --git a/explorer/testdata/function/auto_return/modify_return_type.carbon b/explorer/testdata/function/auto_return/modify_return_type.carbon deleted file mode 100644 index 05d26c5de5c8..000000000000 --- a/explorer/testdata/function/auto_return/modify_return_type.carbon +++ /dev/null @@ -1,18 +0,0 @@ -// 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 -// -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 - -package ExplorerTest api; - -fn Id(t: Type) -> auto { return t; } - -// Test non-trivial type expression in return type. - -fn Main() -> Id(i32) { - return 0; -} diff --git a/explorer/testdata/function/auto_return/type.carbon b/explorer/testdata/function/auto_return/type.carbon deleted file mode 100644 index d0d4c9a04287..000000000000 --- a/explorer/testdata/function/auto_return/type.carbon +++ /dev/null @@ -1,19 +0,0 @@ -// 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 -// -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 - -package ExplorerTest api; - -fn Id(t: Type) -> auto { return t; } - -// Test non-trivial type expression in variable declaration statement. - -fn Main() -> i32 { - var x: Id(i32) = 0; - return x; -} diff --git a/explorer/testdata/function/auto_return/modify_arg_type.carbon b/explorer/testdata/function/fail_param_type_is_call.carbon similarity index 60% rename from explorer/testdata/function/auto_return/modify_arg_type.carbon rename to explorer/testdata/function/fail_param_type_is_call.carbon index 4f9cdb9e4867..c163536c9b2f 100644 --- a/explorer/testdata/function/auto_return/modify_arg_type.carbon +++ b/explorer/testdata/function/fail_param_type_is_call.carbon @@ -2,10 +2,9 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s +// RUN: %{not} %{explorer} %s 2>&1 | %{FileCheck-strict} %s +// RUN: %{not} %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s // AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 package ExplorerTest api; @@ -13,6 +12,7 @@ fn Id(t: Type) -> auto { return t; } // Test non-trivial type expression in parameter type. +// CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/function/fail_param_type_is_call.carbon:[[@LINE+1]]: Function would be called at compile-time fn f(x: Id(i32)) -> i32 { return x; } diff --git a/explorer/testdata/function/fail_return_type_is_call.carbon b/explorer/testdata/function/fail_return_type_is_call.carbon new file mode 100644 index 000000000000..daae9ebcd27d --- /dev/null +++ b/explorer/testdata/function/fail_return_type_is_call.carbon @@ -0,0 +1,20 @@ +// 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 +// +// RUN: %{not} %{explorer} %s 2>&1 2>&1 | %{FileCheck} %s +// AUTOUPDATE: %{explorer} %s + +package EmptyIdentifier impl; + +fn apply[T:! Type, U:! Type](f: T, EmptyIdentifier: U) +{ +match (true[true]) {} +} + +// CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/function/fail_return_type_is_call.carbon:[[@LINE+1]]: Function would be called at compile-time +fn EmptyIdentifier() -> apply(true, true); + +fn Main() -> i32 { + return 0; +} diff --git a/explorer/testdata/function/fail_var_type_is_call.carbon b/explorer/testdata/function/fail_var_type_is_call.carbon new file mode 100644 index 000000000000..13e9199f8a61 --- /dev/null +++ b/explorer/testdata/function/fail_var_type_is_call.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 +// +// RUN: %{not} %{explorer} %s 2>&1 2>&1 | %{FileCheck} %s +// AUTOUPDATE: %{explorer} %s + +package EmptyIdentifier impl; + +fn test() -> i32 { + return 1; +} + +fn Main() -> i32 { + // CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/function/fail_var_type_is_call.carbon:[[@LINE+1]]: Function would be called at compile-time + var x: test() = 1; + return 0; +} diff --git a/explorer/testdata/generic_class/use_at_compile_time.carbon b/explorer/testdata/generic_class/fail_use_at_compile_time.carbon similarity index 63% rename from explorer/testdata/generic_class/use_at_compile_time.carbon rename to explorer/testdata/generic_class/fail_use_at_compile_time.carbon index 113bd83321b2..abe978510bab 100644 --- a/explorer/testdata/generic_class/use_at_compile_time.carbon +++ b/explorer/testdata/generic_class/fail_use_at_compile_time.carbon @@ -2,10 +2,9 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s +// RUN: %{not} %{explorer} %s 2>&1 | %{FileCheck-strict} %s +// RUN: %{not} %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s // AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 package ExplorerTest api; @@ -21,6 +20,7 @@ class WithType(T:! Has(Type)) { } fn Main() -> i32 { + // CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/generic_class/fail_use_at_compile_time.carbon:[[@LINE+1]]: Function would be called at compile-time var v: WithType(i32).Get() = 0; return v; } diff --git a/explorer/testdata/generic_class/instantiation_at_compile_time.carbon b/explorer/testdata/generic_class/instantiation_at_compile_time.carbon deleted file mode 100644 index b133d3c5ff01..000000000000 --- a/explorer/testdata/generic_class/instantiation_at_compile_time.carbon +++ /dev/null @@ -1,22 +0,0 @@ -// 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 -// -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 - -package ExplorerTest api; - -class C(T:! Type) {} - -fn F(T:! Type) -> Type { - return C(T); -} - -fn Main() -> i32 { - var v: F(i32) = {}; - var w: C(i32) = v; - return 0; -} diff --git a/explorer/testdata/generic_function/call_at_compile_time.carbon b/explorer/testdata/generic_function/fail_call_at_compile_time.carbon similarity index 58% rename from explorer/testdata/generic_function/call_at_compile_time.carbon rename to explorer/testdata/generic_function/fail_call_at_compile_time.carbon index d8558c870f99..054c89b543cd 100644 --- a/explorer/testdata/generic_function/call_at_compile_time.carbon +++ b/explorer/testdata/generic_function/fail_call_at_compile_time.carbon @@ -2,10 +2,9 @@ // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s +// RUN: %{not} %{explorer} %s 2>&1 | %{FileCheck-strict} %s +// RUN: %{not} %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s // AUTOUPDATE: %{explorer} %s -// CHECK:result: 0 package ExplorerTest api; @@ -18,6 +17,7 @@ fn G[T:! X](v: T) -> Type { } fn Main() -> i32 { + // CHECK:COMPILATION ERROR: {{.*}}/explorer/testdata/generic_function/fail_call_at_compile_time.carbon:[[@LINE+1]]: Function would be called at compile-time var v: G(0) = 0; return v; } diff --git a/explorer/testdata/member_access/evaluate_type_before_dot.carbon b/explorer/testdata/member_access/evaluate_type_before_dot.carbon deleted file mode 100644 index e5acc39c2dbd..000000000000 --- a/explorer/testdata/member_access/evaluate_type_before_dot.carbon +++ /dev/null @@ -1,76 +0,0 @@ -// 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 -// -// RUN: %{explorer} %s 2>&1 | %{FileCheck-strict} %s -// RUN: %{explorer-trace} %s 2>&1 | %{FileCheck-allow-unmatched} %s -// AUTOUPDATE: %{explorer} %s -// CHECK:Struct OK -// CHECK:Choice OK -// CHECK:Class OK -// CHECK:Interface OK -// CHECK:Constraint OK -// CHECK:result: 0 - -package Foo api; - -choice Choice { Alternative() } -class Class { fn F(n: i32) -> i32 { return n + 1; } } -interface Interface { fn G[me: Self]() -> Self; } -interface AnotherInterface {} - -impl i32 as Interface { fn G[me: i32]() -> i32 { return me + 1; } } -impl i32 as AnotherInterface {} - -// TODO: These are intended to be called at compile time. Mark them as -// constexpr once we have syntax for that. -fn GetStruct() -> Type { return {.n: i32}; } -fn GetChoice() -> Type { return Choice; } -fn GetClass() -> Type { return Class; } -fn GetInterface() -> Type { return Interface; } -fn GetConstraint() -> Type { return Interface & AnotherInterface; } - -fn TestStruct() { - var s: GetStruct() = {.n = 1}; - if (s.(GetStruct().n) == 1) { - Print("Struct OK"); - } -} - -fn TestChoice() { - var c: GetChoice() = GetChoice().Alternative(); - match (c) { - case GetChoice().Alternative() => { - Print("Choice OK"); - } - } -} - -fn TestClass() { - if (GetClass().F(1) == 2) { - Print("Class OK"); - } -} - -fn TestInterface() { - var n: i32 = 1; - if (n.(GetInterface().G)() == 2) { - Print("Interface OK"); - } -} - -fn TestConstraint() { - var n: i32 = 1; - if (n.(GetConstraint().G)() == 2) { - Print("Constraint OK"); - } -} - -fn Main() -> i32 { - TestStruct(); - TestChoice(); - TestClass(); - TestInterface(); - TestConstraint(); - return 0; -}