validation: type of a let must be constructible

This forbids let declarations from having handle types.

Change-Id: I6f7467b0fa3963711ec705e1a81bfdd2c550feee
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/59801
Auto-Submit: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price
2021-07-26 22:11:58 +00:00
parent 9a7ca38a86
commit d12379a405
7 changed files with 22 additions and 102 deletions

View File

@@ -1070,6 +1070,14 @@ bool Resolver::ValidateVariable(const VariableInfo* info) {
return false;
}
if (var->is_const() && info->kind != VariableKind::kParameter &&
!(storage_type->IsConstructible() || storage_type->Is<sem::Pointer>())) {
AddError(storage_type->FriendlyName(builder_->Symbols()) +
" cannot be used as the type of a let",
var->source());
return false;
}
if (auto* r = storage_type->As<sem::Array>()) {
if (r->IsRuntimeSized()) {
AddError("runtime arrays may only appear as the last member of a struct",

View File

@@ -78,6 +78,20 @@ TEST_F(ResolverVarLetValidationTest, VarTypeNotStorable) {
"type of a var");
}
TEST_F(ResolverVarLetValidationTest, LetTypeNotConstructible) {
// [[group(0), binding(0)]] var t1 : texture_2d<f32>;
// let t2 : t1;
auto* t1 =
Global("t1", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()),
GroupAndBinding(0, 0));
auto* t2 = Const(Source{{56, 78}}, "t2", nullptr, Expr(t1));
WrapInFunction(t2);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"56:78 error: texture_2d<f32> cannot be used as the type of a let");
}
TEST_F(ResolverVarLetValidationTest, LetConstructorWrongType) {
// var v : i32 = 2u
WrapInFunction(Const(Source{{3, 3}}, "v", ty.i32(), Expr(2u)));