From 56b47bcfa92da429f046c2fcd5f7386622da3717 Mon Sep 17 00:00:00 2001 From: "Jeremy G. Siek" Date: Wed, 5 May 2021 14:46:10 -0400 Subject: [PATCH] Type computation tests (#511) * test of a little type computation * another test and some bug fixes --- executable_semantics/BUILD | 3 +++ executable_semantics/interpreter/interpreter.cpp | 3 +-- executable_semantics/interpreter/typecheck.cpp | 9 +++++---- executable_semantics/interpreter/value.cpp | 1 - executable_semantics/testdata/tuple5.golden | 4 ++-- .../testdata/tuple_equality3.golden | 4 ++-- executable_semantics/testdata/type_compute.6c | 12 ++++++++++++ executable_semantics/testdata/type_compute.golden | 1 + executable_semantics/testdata/type_compute2.6c | 11 +++++++++++ .../testdata/type_compute2.golden | 1 + executable_semantics/testdata/type_compute3.6c | 15 +++++++++++++++ .../testdata/type_compute3.golden | 1 + 12 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 executable_semantics/testdata/type_compute.6c create mode 100644 executable_semantics/testdata/type_compute.golden create mode 100644 executable_semantics/testdata/type_compute2.6c create mode 100644 executable_semantics/testdata/type_compute2.golden create mode 100644 executable_semantics/testdata/type_compute3.6c create mode 100644 executable_semantics/testdata/type_compute3.golden diff --git a/executable_semantics/BUILD b/executable_semantics/BUILD index df7d39f254bc..aa0653de86a6 100644 --- a/executable_semantics/BUILD +++ b/executable_semantics/BUILD @@ -76,6 +76,9 @@ EXAMPLES = [ "tuple3", "tuple4", "tuple5", + "type_compute", + "type_compute2", + "type_compute3", "while1", "zero", "experimental_continuation1", diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 0e22dbf15987..b53cdd0e50e3 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -333,8 +333,7 @@ auto StructDeclaration::InitGlobals(Env& globals) const -> void { } auto FunctionDeclaration::InitGlobals(Env& globals) const -> void { - Env values; - auto pt = InterpExp(values, definition->param_pattern); + auto pt = InterpExp(globals, definition->param_pattern); auto f = Value::MakeFunVal(definition->name, pt, definition->body); Address a = state->heap.AllocateValue(f); globals.Set(definition->name, a); diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index 6b79a5a5342e..ef59fea74332 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -391,15 +391,15 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values, } } case ExpressionKind::IntT: - return TCResult(e, Value::MakeIntTypeVal(), types); + return TCResult(e, Value::MakeTypeTypeVal(), types); case ExpressionKind::BoolT: - return TCResult(e, Value::MakeBoolTypeVal(), types); + return TCResult(e, Value::MakeTypeTypeVal(), types); case ExpressionKind::TypeT: return TCResult(e, Value::MakeTypeTypeVal(), types); case ExpressionKind::AutoT: - return TCResult(e, Value::MakeAutoTypeVal(), types); + return TCResult(e, Value::MakeTypeTypeVal(), types); case ExpressionKind::ContinuationT: - return TCResult(e, Value::MakeContinuationTypeVal(), types); + return TCResult(e, Value::MakeTypeTypeVal(), types); } } @@ -734,6 +734,7 @@ auto TopLevel(std::list* fs) -> TypeCheckContext { auto FunctionDeclaration::TopLevel(TypeCheckContext& tops) const -> void { auto t = TypeOfFunDef(tops.types, tops.values, definition); tops.types.Set(Name(), t); + InitGlobals(tops.values); } auto StructDeclaration::TopLevel(TypeCheckContext& tops) const -> void { diff --git a/executable_semantics/interpreter/value.cpp b/executable_semantics/interpreter/value.cpp index c36a462eb63f..6c1bb728ed18 100644 --- a/executable_semantics/interpreter/value.cpp +++ b/executable_semantics/interpreter/value.cpp @@ -325,7 +325,6 @@ auto PrintValue(const Value* val, std::ostream& out) -> void { out << element.name << " = "; state->heap.PrintAddress(element.address, out); - out << "@" << element.address; } out << ")"; break; diff --git a/executable_semantics/testdata/tuple5.golden b/executable_semantics/testdata/tuple5.golden index bd957fe75194..4085611ccce4 100644 --- a/executable_semantics/testdata/tuple5.golden +++ b/executable_semantics/testdata/tuple5.golden @@ -1,4 +1,4 @@ 8: type error in pattern variable -expected: (x = Int@2, y = Int@3) -actual: (y = Int@0, x = Int@1) +expected: (x = Int, y = Int) +actual: (y = Int, x = Int) EXIT CODE: 255 diff --git a/executable_semantics/testdata/tuple_equality3.golden b/executable_semantics/testdata/tuple_equality3.golden index 1917c6bbe8d6..f68b9333947a 100644 --- a/executable_semantics/testdata/tuple_equality3.golden +++ b/executable_semantics/testdata/tuple_equality3.golden @@ -1,4 +1,4 @@ 8: type error in == -expected: (0 = Int@2, 1 = Int@3) -actual: (0 = Int@5) +expected: (0 = Int, 1 = Int) +actual: (0 = Int) EXIT CODE: 255 diff --git a/executable_semantics/testdata/type_compute.6c b/executable_semantics/testdata/type_compute.6c new file mode 100644 index 000000000000..fa4f9bf68cfa --- /dev/null +++ b/executable_semantics/testdata/type_compute.6c @@ -0,0 +1,12 @@ +// 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 + +fn Id(Type: t) => t; + +// Test non-trivial type expression in variable declaration statement. + +fn main() -> Int { + var Id(Int): x = 0; + return x; +} diff --git a/executable_semantics/testdata/type_compute.golden b/executable_semantics/testdata/type_compute.golden new file mode 100644 index 000000000000..2db2755da426 --- /dev/null +++ b/executable_semantics/testdata/type_compute.golden @@ -0,0 +1 @@ +result: 0 diff --git a/executable_semantics/testdata/type_compute2.6c b/executable_semantics/testdata/type_compute2.6c new file mode 100644 index 000000000000..ff50e2cadcbc --- /dev/null +++ b/executable_semantics/testdata/type_compute2.6c @@ -0,0 +1,11 @@ +// 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 + +fn Id(Type: t) => t; + +// Test non-trivial type expression in return type. + +fn main() -> Id(Int) { + return 0; +} diff --git a/executable_semantics/testdata/type_compute2.golden b/executable_semantics/testdata/type_compute2.golden new file mode 100644 index 000000000000..2db2755da426 --- /dev/null +++ b/executable_semantics/testdata/type_compute2.golden @@ -0,0 +1 @@ +result: 0 diff --git a/executable_semantics/testdata/type_compute3.6c b/executable_semantics/testdata/type_compute3.6c new file mode 100644 index 000000000000..9135f477c5d8 --- /dev/null +++ b/executable_semantics/testdata/type_compute3.6c @@ -0,0 +1,15 @@ +// 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 + +fn Id(Type: t) => t; + +// Test non-trivial type expression in parameter type. + +fn f(Id(Int): x) -> Int { + return x; +} + +fn main() -> Int { + return f(0); +} diff --git a/executable_semantics/testdata/type_compute3.golden b/executable_semantics/testdata/type_compute3.golden new file mode 100644 index 000000000000..2db2755da426 --- /dev/null +++ b/executable_semantics/testdata/type_compute3.golden @@ -0,0 +1 @@ +result: 0