diff --git a/src/resolver/ptr_ref_validation_test.cc b/src/resolver/ptr_ref_validation_test.cc index c693d19f1a..73b1f4606d 100644 --- a/src/resolver/ptr_ref_validation_test.cc +++ b/src/resolver/ptr_ref_validation_test.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "src/ast/bitcast_expression.h" #include "src/ast/struct_block_decoration.h" #include "src/resolver/resolver.h" #include "src/resolver/resolver_test_helper.h" @@ -114,6 +115,19 @@ TEST_F(ResolverPtrRefValidationTest, InferredPtrAccessMismatch) { "'ptr'"); } +TEST_F(ResolverTest, Expr_Bitcast_ptr) { + auto* vf = Var("vf", ty.f32()); + auto* bitcast = create( + Source{{12, 34}}, ty.pointer(ast::StorageClass::kFunction), + Expr("vf")); + auto* ip = + Const("ip", ty.pointer(ast::StorageClass::kFunction), bitcast); + WrapInFunction(Decl(vf), Decl(ip)); + + EXPECT_FALSE(r()->Resolve()); + EXPECT_EQ(r()->error(), "12:34 error: cannot cast to a pointer"); +} + } // namespace } // namespace resolver } // namespace tint diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 9b2dc9efc1..028f988308 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -1680,6 +1680,10 @@ bool Resolver::Bitcast(ast::BitcastExpression* expr) { if (!ty) { return false; } + if (ty->Is()) { + diagnostics_.add_error("cannot cast to a pointer", expr->source()); + return false; + } SetType(expr, ty, expr->type()->FriendlyName(builder_->Symbols())); return true; } @@ -1861,6 +1865,10 @@ bool Resolver::Constructor(ast::ConstructorExpression* expr) { // Now that the argument types have been determined, make sure that they // obey the constructor type rules laid out in // https://gpuweb.github.io/gpuweb/wgsl.html#type-constructor-expr. + if (type->Is()) { + diagnostics_.add_error("cannot cast to a pointer", expr->source()); + return false; + } if (auto* vec_type = type->As()) { return ValidateVectorConstructor(type_ctor, vec_type); } diff --git a/src/resolver/validation_test.cc b/src/resolver/validation_test.cc index 83f6488c38..d71aa7d517 100644 --- a/src/resolver/validation_test.cc +++ b/src/resolver/validation_test.cc @@ -728,6 +728,18 @@ TEST_F(ResolverValidationTest, OffsetAndAlignAndSizeDecoration) { "decorations"); } +TEST_F(ResolverTest, Expr_Constructor_Cast_Pointer) { + auto* vf = Var("vf", ty.f32()); + auto* c = create( + Source{{12, 34}}, ty.pointer(ast::StorageClass::kFunction), + ExprList(vf)); + auto* ip = Const("ip", ty.pointer(ast::StorageClass::kFunction), c); + WrapInFunction(Decl(vf), Decl(ip)); + + EXPECT_FALSE(r()->Resolve()); + EXPECT_EQ(r()->error(), "12:34 error: cannot cast to a pointer"); +} + TEST_F(ResolverValidationTest, Expr_Constructor_Array_ZeroValue_Pass) { // array(); auto* tc = array();