فهرست منبع

Restructure __intrinsic_print to be a keyword-like Print (#1407)

Jon Ross-Perkins 3 سال پیش
والد
کامیت
084bc6ebc1

+ 4 - 0
explorer/ast/expression.cpp

@@ -22,6 +22,10 @@ using llvm::isa;
 auto IntrinsicExpression::FindIntrinsic(std::string_view name,
                                         SourceLocation source_loc)
     -> ErrorOr<Intrinsic> {
+  // 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<std::string_view, Intrinsic>(
       {{"print", Intrinsic::Print},
        {"new", Intrinsic::Alloc},

+ 4 - 8
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* {

+ 2 - 1
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]

+ 3 - 3
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;
 }

+ 4 - 4
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;
 }

+ 3 - 3
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 **********

+ 17 - 0
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;
+}

+ 19 - 0
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;
+}

+ 17 - 0
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;
+}

+ 19 - 0
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;
+}

+ 0 - 0
explorer/testdata/basic_syntax/print.carbon → explorer/testdata/print/format_only.carbon


+ 1 - 1
explorer/testdata/basic_syntax/print_i32.carbon → 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;
 }