From cdfea42f1b28c6ec7b13500a64be823f67bf8e0a Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Fri, 7 Jul 2023 03:52:00 +0800 Subject: Fix erroneous error claiming variable is being used before its declaration (#2958) * Simplify type of diagnoseImpl * Show source line for Note diagnostics, opting out of this where appropriate * Make declared after use diagnostic clearer * Fix erroneous error claiming variable is being used before its declaration Closes https://github.com/shader-slang/slang/issues/2936 * Fix build on msvc --------- Co-authored-by: jsmall-nvidia --- source/slang/slang-lookup.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'source/slang/slang-lookup.cpp') diff --git a/source/slang/slang-lookup.cpp b/source/slang/slang-lookup.cpp index 0c4279761..46977b71d 100644 --- a/source/slang/slang-lookup.cpp +++ b/source/slang/slang-lookup.cpp @@ -139,6 +139,13 @@ static void _lookUpMembersInValue( LookupResult& ioResult, BreadcrumbInfo* breadcrumbs); +static bool _isUncheckedLocalVar(const Decl* decl) +{ + auto checkStateExt = decl->checkState; + auto isUnchecked = checkStateExt.getState() == DeclCheckState::Unchecked || checkStateExt.isBeingChecked(); + return isUnchecked && isLocalVar(decl); +} + /// Look up direct members (those declared in `containerDeclRef` itself, as well /// as transitively through any direct members that are marked "transparent." /// @@ -162,6 +169,8 @@ static void _lookUpDirectAndTransparentMembers( // return all the members that are available. for (auto member : containerDecl->members) { + if(!request.shouldConsiderAllLocalNames() && _isUncheckedLocalVar(member)) + continue; if (!DeclPassesLookupMask(member, request.mask)) continue; AddToLookupResult( @@ -182,6 +191,12 @@ static void _lookUpDirectAndTransparentMembers( // type declarations. for (auto m = firstDecl; m; m = m->nextInContainerWithSameName) { + // Skip this declaration if we are checking and this hasn't been + // checked yet. Because we traverse block statements in order, if + // it's unchecked or being checked then it isn't declared yet. + if(!request.shouldConsiderAllLocalNames() && _isUncheckedLocalVar(m)) + continue; + if (!DeclPassesLookupMask(m, request.mask)) continue; @@ -948,10 +963,14 @@ LookupResult lookUp( SemanticsVisitor* semantics, Name* name, Scope* scope, - LookupMask mask) + LookupMask mask, + bool considerAllLocalNamesInScope) { LookupResult result; - LookupRequest request = initLookupRequest(semantics, name, mask, LookupOptions::None, scope); + const auto options = considerAllLocalNamesInScope + ? LookupOptions::ConsiderAllLocalNamesInScope + : LookupOptions::None; + LookupRequest request = initLookupRequest(semantics, name, mask, options, scope); _lookUpInScopes(astBuilder, name, request, result); return result; } -- cgit v1.2.3