diff --git a/explorer/syntax/parser.ypp b/explorer/syntax/parser.ypp index e01472abf95f..638d5297ff93 100644 --- a/explorer/syntax/parser.ypp +++ b/explorer/syntax/parser.ypp @@ -59,9 +59,9 @@ #include #include - #include "common/check.h" #include "explorer/syntax/parse_and_lex_context.h" #include "llvm/ADT/StringExtras.h" + #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/raw_ostream.h" } // %code top @@ -349,11 +349,18 @@ primary_expression: { $$ = arena->New(context.source_loc(), false); } | sized_type_literal { - int val; - CARBON_CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val)); - CARBON_CHECK($1[0] == 'i' && val == 32) - << "Only i32 is supported for now: " << $1; - $$ = arena->New(context.source_loc()); + int val = 0; + if (!llvm::to_integer(llvm::StringRef($1).substr(1), val)) { + context.RecordSyntaxError( + llvm::formatv("Invalid type literal: {0}", $1)); + YYERROR; + } else if ($1[0] != 'i' || val != 32) { + context.RecordSyntaxError( + llvm::formatv("Only i32 is supported for now: {0}", $1)); + YYERROR; + } else { + $$ = arena->New(context.source_loc()); + } } | SELF // TODO: Should we create a new TypeLiteral for `Self`? diff --git a/explorer/testdata/basic_syntax/fail_invalid_integer_type.carbon b/explorer/testdata/basic_syntax/fail_invalid_integer_type.carbon new file mode 100644 index 000000000000..be70267d3aec --- /dev/null +++ b/explorer/testdata/basic_syntax/fail_invalid_integer_type.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/basic_syntax/fail_invalid_integer_type.carbon:[[@LINE+1]]: Invalid type literal: i11111111111111111111111111 + var x: i11111111111111111111111111 = 1; + return 0; +} diff --git a/explorer/testdata/basic_syntax/fail_unsupported_integer_type.carbon b/explorer/testdata/basic_syntax/fail_unsupported_integer_type.carbon new file mode 100644 index 000000000000..e10d6c0a7655 --- /dev/null +++ b/explorer/testdata/basic_syntax/fail_unsupported_integer_type.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/basic_syntax/fail_unsupported_integer_type.carbon:[[@LINE+1]]: Only i32 is supported for now: i64 + var x: i64 = 1; + return 0; +}