2021-03-25 12:55:27 +00:00
|
|
|
// Copyright 2021 The Tint Authors.
|
2020-09-02 19:58:06 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/ast/fallthrough_statement.h"
|
2021-03-25 12:55:27 +00:00
|
|
|
#include "src/ast/switch_statement.h"
|
|
|
|
#include "src/resolver/resolver_test_helper.h"
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
class ResolverControlBlockValidationTest : public resolver::TestHelper,
|
|
|
|
public testing::Test {};
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
|
|
|
SwitchSelectorExpressionNoneIntegerType_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : f32 = 3.14;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.f32(), ast::StorageClass::kNone, Expr(3.14f));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList body;
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
body.push_back(
|
|
|
|
create<ast::CaseStatement>(ast::CaseSelectorList{}, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr(Source{Source::Location{12, 34}}, "a"),
|
|
|
|
body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(r()->error(),
|
|
|
|
"12:34 error v-0025: switch statement selector expression must be "
|
2020-09-02 19:58:06 +00:00
|
|
|
"of a scalar integer type");
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest, SwitchWithoutDefault_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case 1: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
csl.push_back(Literal(1));
|
2020-12-16 21:38:40 +00:00
|
|
|
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-12-12 11:58:44 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(
|
2020-12-15 14:10:28 +00:00
|
|
|
csl, create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Source{Source::Location{12, 34}}, Expr("a"),
|
2020-12-15 14:10:28 +00:00
|
|
|
body),
|
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(r()->error(),
|
|
|
|
"12:34 error: switch statement must have a default clause");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest, SwitchWithTwoDefault_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// case 1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
|
|
|
ast::CaseSelectorList default_csl_1;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default_1 = create<ast::BlockStatement>(ast::StatementList{});
|
2020-11-16 16:41:47 +00:00
|
|
|
switch_body.push_back(
|
2020-12-15 14:10:28 +00:00
|
|
|
create<ast::CaseStatement>(default_csl_1, block_default_1));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_case_1;
|
2020-12-15 14:10:28 +00:00
|
|
|
csl_case_1.push_back(Literal(1));
|
|
|
|
auto* block_case_1 = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
switch_body.push_back(create<ast::CaseStatement>(csl_case_1, block_case_1));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl_2;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default_2 = create<ast::BlockStatement>(ast::StatementList{});
|
2021-03-25 12:55:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, default_csl_2, block_default_2));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2021-03-25 12:55:27 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), switch_body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(
|
|
|
|
r()->error(),
|
|
|
|
"12:34 error v-0008: switch statement must have exactly one default "
|
|
|
|
"clause");
|
2020-09-10 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
2020-09-10 14:37:17 +00:00
|
|
|
SwitchConditionTypeMustMatchSelectorType2_Fail) {
|
2021-03-25 12:55:27 +00:00
|
|
|
// var a : u32 = 2;
|
2020-09-10 14:37:17 +00:00
|
|
|
// switch (a) {
|
|
|
|
// case 1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
|
|
|
ast::CaseSelectorList csl;
|
2021-01-26 16:57:10 +00:00
|
|
|
csl.push_back(create<ast::UintLiteral>(ty.u32(), 1));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl,
|
2020-12-15 14:10:28 +00:00
|
|
|
create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
switch_body.push_back(create<ast::CaseStatement>(default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), switch_body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(r()->error(),
|
|
|
|
"12:34 error v-0026: the case selector values must have the same "
|
2020-09-10 14:37:17 +00:00
|
|
|
"type as the selector expression.");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
2020-09-10 14:37:17 +00:00
|
|
|
SwitchConditionTypeMustMatchSelectorType_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : u32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case -1: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.u32(), ast::StorageClass::kNone, Expr(2u));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
|
|
|
ast::CaseSelectorList csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
csl.push_back(Literal(-1));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl,
|
2020-12-15 14:10:28 +00:00
|
|
|
create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
switch_body.push_back(create<ast::CaseStatement>(default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), switch_body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(r()->error(),
|
|
|
|
"12:34 error v-0026: the case selector values must have the same "
|
2020-09-10 14:37:17 +00:00
|
|
|
"type as the selector expression.");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
|
|
|
NonUniqueCaseSelectorValueUint_Fail) {
|
2020-09-10 14:37:17 +00:00
|
|
|
// var a : u32 = 3;
|
|
|
|
// switch (a) {
|
|
|
|
// case 0: {}
|
|
|
|
// case 2, 2: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.u32(), ast::StorageClass::kNone, Expr(3u));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
|
|
|
ast::CaseSelectorList csl_1;
|
2021-01-26 16:57:10 +00:00
|
|
|
csl_1.push_back(create<ast::UintLiteral>(ty.u32(), 0));
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
2020-12-15 14:10:28 +00:00
|
|
|
csl_1, create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_2;
|
2021-01-26 16:57:10 +00:00
|
|
|
csl_2.push_back(create<ast::UintLiteral>(ty.u32(), 2));
|
|
|
|
csl_2.push_back(create<ast::UintLiteral>(ty.u32(), 2));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl_2,
|
2020-12-15 14:10:28 +00:00
|
|
|
create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
switch_body.push_back(create<ast::CaseStatement>(default_csl, block_default));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), switch_body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
2021-04-16 10:44:54 +00:00
|
|
|
EXPECT_EQ(r()->error(),
|
|
|
|
"12:34 error v-0027: a literal value must not appear more than "
|
|
|
|
"once in the case selectors for a switch statement: '2u'");
|
2020-09-10 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
|
|
|
NonUniqueCaseSelectorValueSint_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// case 10: {}
|
|
|
|
// case 0,1,2,10: {}
|
|
|
|
// default: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseStatementList switch_body;
|
|
|
|
ast::CaseSelectorList csl_1;
|
2020-12-15 14:10:28 +00:00
|
|
|
csl_1.push_back(Literal(10));
|
2020-12-12 11:58:44 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
2020-12-15 14:10:28 +00:00
|
|
|
csl_1, create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList csl_2;
|
2020-12-15 14:10:28 +00:00
|
|
|
csl_2.push_back(Literal(0));
|
|
|
|
csl_2.push_back(Literal(1));
|
|
|
|
csl_2.push_back(Literal(2));
|
|
|
|
csl_2.push_back(Literal(10));
|
2020-12-14 20:25:27 +00:00
|
|
|
switch_body.push_back(create<ast::CaseStatement>(
|
|
|
|
Source{Source::Location{12, 34}}, csl_2,
|
2020-12-15 14:10:28 +00:00
|
|
|
create<ast::BlockStatement>(ast::StatementList{})));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
switch_body.push_back(create<ast::CaseStatement>(default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), switch_body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(
|
|
|
|
r()->error(),
|
|
|
|
"12:34 error v-0027: a literal value must not appear more than once in "
|
|
|
|
"the case selectors for a switch statement: '10'");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest,
|
|
|
|
LastClauseLastStatementIsFallthrough_Fail) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: { fallthrough; }
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-14 20:25:27 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(
|
2020-12-15 14:10:28 +00:00
|
|
|
|
2020-12-14 20:25:27 +00:00
|
|
|
ast::StatementList{
|
|
|
|
create<ast::FallthroughStatement>(Source{Source::Location{12, 34}}),
|
|
|
|
});
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-12-15 14:10:28 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(default_csl, block_default));
|
|
|
|
|
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_FALSE(r()->Resolve());
|
|
|
|
EXPECT_EQ(
|
|
|
|
r()->error(),
|
|
|
|
"12:34 error v-0028: a fallthrough statement must not appear as the "
|
|
|
|
"last statement in last clause of a switch");
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest, SwitchCase_Pass) {
|
2020-09-02 19:58:06 +00:00
|
|
|
// var a : i32 = 2;
|
|
|
|
// switch (a) {
|
|
|
|
// default: {}
|
|
|
|
// case 5: {}
|
|
|
|
// }
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2));
|
2020-09-02 19:58:06 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-11-13 22:18:35 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(Source{Source::Location{12, 34}},
|
2020-11-16 16:41:47 +00:00
|
|
|
default_csl, block_default));
|
2020-09-02 19:58:06 +00:00
|
|
|
ast::CaseSelectorList case_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
case_csl.push_back(Literal(5));
|
|
|
|
auto* block_case = create<ast::BlockStatement>(ast::StatementList{});
|
|
|
|
body.push_back(create<ast::CaseStatement>(case_csl, block_case));
|
|
|
|
|
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
2020-09-02 19:58:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
TEST_F(ResolverControlBlockValidationTest, SwitchCaseAlias_Pass) {
|
2020-09-10 14:37:17 +00:00
|
|
|
// type MyInt = u32;
|
2020-09-21 17:56:41 +00:00
|
|
|
// var v: MyInt;
|
|
|
|
// switch(v){
|
|
|
|
// default: {}
|
2020-09-10 14:37:17 +00:00
|
|
|
// }
|
2020-09-21 17:56:41 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* my_int = ty.alias("MyInt", ty.u32());
|
2021-02-17 00:26:52 +00:00
|
|
|
auto* var = Var("a", my_int, ast::StorageClass::kNone, Expr(2u));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
|
|
|
ast::CaseSelectorList default_csl;
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block_default = create<ast::BlockStatement>(ast::StatementList{});
|
2020-09-10 14:37:17 +00:00
|
|
|
ast::CaseStatementList body;
|
2020-11-13 22:18:35 +00:00
|
|
|
body.push_back(create<ast::CaseStatement>(Source{Source::Location{12, 34}},
|
2020-11-16 16:41:47 +00:00
|
|
|
default_csl, block_default));
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2020-12-15 14:10:28 +00:00
|
|
|
auto* block = create<ast::BlockStatement>(ast::StatementList{
|
|
|
|
create<ast::VariableDeclStatement>(var),
|
2020-12-16 21:38:40 +00:00
|
|
|
create<ast::SwitchStatement>(Expr("a"), body),
|
2020-12-15 14:10:28 +00:00
|
|
|
});
|
2021-01-26 16:57:10 +00:00
|
|
|
AST().AddConstructedType(my_int);
|
2020-09-10 14:37:17 +00:00
|
|
|
|
2021-02-03 17:19:59 +00:00
|
|
|
WrapInFunction(block);
|
2021-01-21 16:15:00 +00:00
|
|
|
|
2021-03-25 12:55:27 +00:00
|
|
|
EXPECT_TRUE(r()->Resolve()) << r()->error();
|
2020-09-10 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:58:06 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace tint
|