From 1e6382c564629f8a3998800f0273a44c7cb84c14 Mon Sep 17 00:00:00 2001 From: "A. R. Shajii" Date: Tue, 3 Oct 2023 17:04:15 -0400 Subject: [PATCH] Fix domination within deeply nested blocks (#485) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ibrahim Numanagić --- codon/parser/visitors/simplify/ctx.cpp | 9 ++++++-- test/parser/types.codon | 32 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/codon/parser/visitors/simplify/ctx.cpp b/codon/parser/visitors/simplify/ctx.cpp index 35b1112f..cecc53f8 100644 --- a/codon/parser/visitors/simplify/ctx.cpp +++ b/codon/parser/visitors/simplify/ctx.cpp @@ -126,11 +126,16 @@ SimplifyContext::Item SimplifyContext::findDominatingBinding(const std::string & // We went outside the function scope. Break. if (!isOutside && (*i)->getBaseName() != getBaseName()) break; + bool completeDomination = + (*i)->scope.size() <= scope.blocks.size() && + (*i)->scope.back() == scope.blocks[(*i)->scope.size() - 1]; + if (!completeDomination && prefix < int(scope.blocks.size()) && prefix != p) { + break; + } prefix = p; lastGood = i; // The binding completely dominates the current scope. Break. - if ((*i)->scope.size() <= scope.blocks.size() && - (*i)->scope.back() == scope.blocks[(*i)->scope.size() - 1]) + if (completeDomination) break; } seqassert(lastGood != it->second.end(), "corrupted scoping ({})", name); diff --git a/test/parser/types.codon b/test/parser/types.codon index c90d0b04..3852deb1 100644 --- a/test/parser/types.codon +++ b/test/parser/types.codon @@ -1998,3 +1998,35 @@ print(Tuple[-5, int].__class__.__name__) #: Tuple print(Tuple[5, int, str].__class__.__name__) #: Tuple[int,str,int,str,int,str,int,str,int,str] + + +#%% domination_nested,barebones +def correlate(a, b, mode = 'valid'): + if mode == 'valid': + if isinstance(a, List): + xret = '1' + else: + xret = '2' + for i in a: + for j in b: + xret += 'z' + elif mode == 'same': + if isinstance(a, List): + xret = '3' + else: + xret = '4' + for i in a: + for j in b: + xret += 'z' + elif mode == 'full': + if isinstance(a, List): + xret = '5' + else: + xret = '6' + for i in a: + for j in b: + xret += 'z' + else: + raise ValueError(f"mode must be one of 'valid', 'same', or 'full' (got {repr(mode)})") + return xret +print(correlate([1], [2], 'full')) # 5z