From 2f782d403ae5729b6c93fbe92577ee01f7a8d608 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 20 Apr 2018 16:56:33 -0700 Subject: Diagnose use of an implicit cast as an argument for an `out` parameter (#516) Work on #499 Two big fixes here: * The logic for checking constraints on `out` arguments wasn't actually triggering because it relied on function parameters being given an `OutType` if they are marked `out`, but the code wasn't actually doing that. Fixing the computation of types for functions resolved that issue. * Next, I added a specific diagnostic to follow up the "expected an l-value" error to let the user know that their argument was implicitly converted, and that is why it doesn't count as an l-value in Slang's rules. I've added a test case to ensure that we retain this diagnostic until we can do a true fix for the issue. The right long-term fix is to have an AST representation of all the implicit casts involved (e.g., in both directions for an `inout` parameter), and then have the IR generate explicit code for the conversions in each direction (the `LoweredVal` representation can handle this sort of thing). --- source/slang/check.cpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 67b628596..acf473998 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -6738,13 +6738,37 @@ namespace Slang auto paramType = funcType->getParamType(pp); if (auto outParamType = paramType->As()) { - if (pp < expr->Arguments.Count() - && !expr->Arguments[pp]->type.IsLeftValue) + if( pp < expr->Arguments.Count() ) { - getSink()->diagnose( - expr->Arguments[pp], - Diagnostics::argumentExpectedLValue, - pp); + auto argExpr = expr->Arguments[pp]; + if( !argExpr->type.IsLeftValue ) + { + getSink()->diagnose( + argExpr, + Diagnostics::argumentExpectedLValue, + pp); + + if( auto implicitCastExpr = argExpr.As() ) + { + getSink()->diagnose( + argExpr, + Diagnostics::implicitCastUsedAsLValue, + implicitCastExpr->Arguments[0]->type, + implicitCastExpr->type); + } + } + } + else + { + // This implies that the function had an `out` + // or `inout` parameter and they gave it a default + // argument expression. I'm not even sure what + // that would mean. + // + // TODO: make sure this gets validated on the + // declaring side. + // + SLANG_DIAGNOSE_UNEXPECTED(getSink(), invoke, "default argument expression for out/inout paameter"); } } } -- cgit v1.2.3