diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 9a3681f7f282..5e43cc767236 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -1421,6 +1421,10 @@ void TypeChecker::DeclareDeclaration(Nonnull d, auto& var = cast(*d); // Associate the variable name with it's declared type in the // compile-time symbol table. + if (!llvm::isa(var.binding().type())) { + FATAL_COMPILATION_ERROR(var.binding().type().source_loc()) + << "Expected expression for variable type"; + } Expression& type = cast(var.binding().type()).expression(); TypeCheckPattern(&var.binding(), std::nullopt, impl_scope); diff --git a/executable_semantics/syntax/lexer.lpp b/executable_semantics/syntax/lexer.lpp index e282e77fe76e..462f1666d74b 100644 --- a/executable_semantics/syntax/lexer.lpp +++ b/executable_semantics/syntax/lexer.lpp @@ -266,8 +266,10 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" {integer_literal} { BEGIN(AFTER_OPERAND); - int val; - CHECK(llvm::to_integer(yytext, val)); + int val = 0; + if (!llvm::to_integer(yytext, val)) { + FATAL_SYNTAX_ERROR(context) << "Invalid integer literal: " << yytext; + } return ARG_TOKEN(integer_literal, val); } diff --git a/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon b/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon new file mode 100644 index 000000000000..0d2389a25a65 --- /dev/null +++ b/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon @@ -0,0 +1,16 @@ +// 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} %{executable_semantics} %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{executable_semantics} --trace %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{executable_semantics} %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon:15: Invalid integer literal: 11111111111111111111111111 + +package ExecutableSemanticsTest api; + +fn Main() -> i32 { + return 11111111111111111111111111; +} diff --git a/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon b/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon new file mode 100644 index 000000000000..dccd4f1ef803 --- /dev/null +++ b/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon @@ -0,0 +1,16 @@ +// 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} %{executable_semantics} %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{executable_semantics} --trace %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{executable_semantics} %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon:14: Expected expression for variable type + +package ExecutableSemanticsTest api; + +var a: auto; + +fn Main() -> i32 { return 0; }