diff --git a/explorer/ast/expression.cpp b/explorer/ast/expression.cpp index 3274855b41ad..280e7d3824ec 100644 --- a/explorer/ast/expression.cpp +++ b/explorer/ast/expression.cpp @@ -22,6 +22,10 @@ using llvm::isa; auto IntrinsicExpression::FindIntrinsic(std::string_view name, SourceLocation source_loc) -> ErrorOr { + // TODO: Remove Print special casing once we have variadics or overloads. + if (name == "Print") { + return Intrinsic::Print; + } static const auto& intrinsic_map = *new std::map( {{"print", Intrinsic::Print}, {"new", Intrinsic::Alloc}, diff --git a/explorer/data/prelude.carbon b/explorer/data/prelude.carbon index e82dd6cc3063..a8c6055a3618 100644 --- a/explorer/data/prelude.carbon +++ b/explorer/data/prelude.carbon @@ -36,14 +36,10 @@ impl forall [U1:! Type, U2:! Type, U3:! Type, // Note that Print is experimental, and not part of an accepted proposal, but // is included here for printing state in tests. -fn Print(format_str: String) { - __intrinsic_print(format_str); -} - -// Neither overloads nor variadics are supported, so using a different name. -fn PrintInt(format_str: String, arg0: i32) { - __intrinsic_print(format_str, arg0); -} +// TODO: Remove Print special casing once we have variadics or overloads. +// fn Print(format_str: String) { +// __intrinsic_print(format_str); +// } class Heap { fn New[T:! Type, me: Self](x : T) -> T* { diff --git a/explorer/syntax/lexer.lpp b/explorer/syntax/lexer.lpp index 20633fa7feff..e18e040f9886 100644 --- a/explorer/syntax/lexer.lpp +++ b/explorer/syntax/lexer.lpp @@ -100,7 +100,8 @@ WHILE "while" /* This should be kept table-like, but isn't automatic due to spaces. */ identifier [A-Za-z_][A-Za-z0-9_]* -intrinsic_identifier __intrinsic_[A-Za-z0-9_]* +/* TODO: Remove Print special casing once we have variadics or overloads. */ +intrinsic_identifier (Print|__intrinsic_[A-Za-z0-9_]*) sized_type_literal [iuf][1-9][0-9]* integer_literal [0-9]+ horizontal_whitespace [ \t\r] diff --git a/explorer/testdata/alias/class_alias.carbon b/explorer/testdata/alias/class_alias.carbon index 62f5c166d842..754fbbe0b710 100644 --- a/explorer/testdata/alias/class_alias.carbon +++ b/explorer/testdata/alias/class_alias.carbon @@ -39,8 +39,8 @@ fn Main() -> i32 { var d: GenericClassAlias(i32) = c; var e: ClassSpecializationAlias = c; - PrintInt("b.n: {0}", b.n); - PrintInt("d.Get(0): {0}", d.Get(0)); - PrintInt("e.Get(1): {0}", e.Get(1)); + Print("b.n: {0}", b.n); + Print("d.Get(0): {0}", d.Get(0)); + Print("e.Get(1): {0}", e.Get(1)); return 0; } diff --git a/explorer/testdata/alias/struct_alias.carbon b/explorer/testdata/alias/struct_alias.carbon index e8771c6f5524..0a400681c3b3 100644 --- a/explorer/testdata/alias/struct_alias.carbon +++ b/explorer/testdata/alias/struct_alias.carbon @@ -21,9 +21,9 @@ alias BA = {.b: i32, .a: i32}; fn Main() -> i32 { var ab: AB = {.b = 1, .a = 2}; var ba: BA = ab; - PrintInt("ab.a: {0}", ab.a); - PrintInt("ab.b: {0}", ab.b); - PrintInt("ba.a: {0}", ba.a); - PrintInt("ba.b: {0}", ba.b); + Print("ab.a: {0}", ab.a); + Print("ab.b: {0}", ab.b); + Print("ba.a: {0}", ba.a); + Print("ba.b: {0}", ba.b); return 0; } diff --git a/explorer/testdata/basic_syntax/trace.carbon b/explorer/testdata/basic_syntax/trace.carbon index 5b676cb219a9..2dfca109f58c 100644 --- a/explorer/testdata/basic_syntax/trace.carbon +++ b/explorer/testdata/basic_syntax/trace.carbon @@ -10,11 +10,11 @@ // // NOAUTOUPDATE // CHECK: ********** source program ********** -// CHECK: fn Print (format_str: String) { +// CHECK: interface ImplicitAs { // CHECK: ********** type checking ********** -// CHECK: checking tuple pattern (format_str: String) +// CHECK: ** declaring interface ImplicitAs // CHECK: ********** type checking complete ********** -// CHECK: fn Print (format_str: String) { +// CHECK: interface ImplicitAs { // CHECK: ********** starting execution ********** // CHECK: ********** initializing globals ********** // CHECK: ********** calling main function ********** diff --git a/explorer/testdata/print/fail_no_args.carbon b/explorer/testdata/print/fail_no_args.carbon new file mode 100644 index 000000000000..ac2ee07032a6 --- /dev/null +++ b/explorer/testdata/print/fail_no_args.carbon @@ -0,0 +1,17 @@ +// 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 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{explorer} --parser_debug --trace_file=- %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{explorer} %s + +package ExplorerTest api; + +fn Main() -> i32 { + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/print/fail_no_args.carbon:[[@LINE+1]]: __intrinsic_print takes 1 or 2 arguments, received 0 + Print(); + return 0; +} diff --git a/explorer/testdata/print/fail_not_str.carbon b/explorer/testdata/print/fail_not_str.carbon new file mode 100644 index 000000000000..e6a7915daa33 --- /dev/null +++ b/explorer/testdata/print/fail_not_str.carbon @@ -0,0 +1,19 @@ +// 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 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{explorer} --parser_debug --trace_file=- %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{explorer} %s + +package ExplorerTest api; + +fn Main() -> i32 { + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/print/fail_not_str.carbon:[[@LINE+3]]: type error in __intrinsic_print argument 0 + // CHECK: expected: String + // CHECK: actual: {.x: i32} + Print({.x = 1}); + return 0; +} diff --git a/explorer/testdata/print/fail_too_many_args.carbon b/explorer/testdata/print/fail_too_many_args.carbon new file mode 100644 index 000000000000..fd31330e9b96 --- /dev/null +++ b/explorer/testdata/print/fail_too_many_args.carbon @@ -0,0 +1,17 @@ +// 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 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{explorer} --parser_debug --trace_file=- %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{explorer} %s + +package ExplorerTest api; + +fn Main() -> i32 { + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/print/fail_too_many_args.carbon:[[@LINE+1]]: __intrinsic_print takes 1 or 2 arguments, received 5 + Print("too", "many", "args", "to", "print"); + return 0; +} diff --git a/explorer/testdata/print/fail_type.carbon b/explorer/testdata/print/fail_type.carbon new file mode 100644 index 000000000000..79db74b7b018 --- /dev/null +++ b/explorer/testdata/print/fail_type.carbon @@ -0,0 +1,19 @@ +// 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 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{explorer} --parser_debug --trace_file=- %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{explorer} %s + +package ExplorerTest api; + +fn Main() -> i32 { + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/print/fail_type.carbon:[[@LINE+3]]: type error in __intrinsic_print argument 1 + // CHECK: expected: i32 + // CHECK: actual: {.x: i32} + Print("{0}", {.x = 1}); + return 0; +} diff --git a/explorer/testdata/basic_syntax/print.carbon b/explorer/testdata/print/format_only.carbon similarity index 100% rename from explorer/testdata/basic_syntax/print.carbon rename to explorer/testdata/print/format_only.carbon diff --git a/explorer/testdata/basic_syntax/print_i32.carbon b/explorer/testdata/print/i32.carbon similarity index 94% rename from explorer/testdata/basic_syntax/print_i32.carbon rename to explorer/testdata/print/i32.carbon index 4632668000a9..d309cc9022eb 100644 --- a/explorer/testdata/basic_syntax/print_i32.carbon +++ b/explorer/testdata/print/i32.carbon @@ -13,6 +13,6 @@ package ExplorerTest api; fn Main() -> i32 { - PrintInt("Printing {0}", 1); + Print("Printing {0}", 1); return 0; }