Skip to content

Commit

Permalink
internal/core/adt: hoist allows from injectClosed
Browse files Browse the repository at this point in the history
The code currently used matchPattern directly
in processComprehesionInner, which really should
use allows.

Hoist the code so it can be shared. We do so in
a separate CL to keep no-op changes separated
from material ones.

We'll fix the bug in a followup CL.

Issue #3486

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I02f743898e33c8e6fdd03fccfc9080a9b63535de
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202420
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mpvl committed Oct 9, 2024
1 parent 89acced commit 6916b5b
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions internal/core/adt/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,14 +957,11 @@ func isTotal(p Value) bool {
// and patterns defined in closed. It reports an error in the nodeContext if
// this is not the case.
func injectClosed(ctx *OpContext, closed, dst *closeContext) {
// TODO: check that fields are not void arcs.
outer:
for _, a := range dst.arcs {
if a.kind != ARC {
continue
}
ca := a.cc
f := ca.Label()
if ca.arcType == ArcPending {
// This happens when a comprehension did not yield any results.
continue
Expand All @@ -980,26 +977,12 @@ outer:
default:
panic("unreachable")
}
f := ca.Label()
// TODO: disallow new definitions in closed structs.
if f.IsHidden() || f.IsLet() || f.IsDef() {
continue
}
for _, b := range closed.arcs {
cb := b.cc
// TODO: we could potentially remove the check for ArcPending if we
// explicitly set the arcType to ArcNonPresent when a comprehension
// yields no results.
if cb.arcType == ArcNotPresent || cb.arcType == ArcPending {
continue
}
if f == cb.Label() {
continue outer
}
}
if !matchPattern(ctx, closed.Expr, ca.Label()) {
ctx.notAllowedError(closed.src, ca.src)
continue
}
closed.allows(ctx, f, ca)
}

if !dst.isClosed {
Expand All @@ -1015,6 +998,26 @@ outer:
}
}

func (c *closeContext) allows(ctx *OpContext, f Feature, ca *closeContext) bool {
for _, b := range c.arcs {
cb := b.cc
// TODO: we could potentially remove the check for ArcPending if we
// explicitly set the arcType to ArcNonPresent when a comprehension
// yields no results.
if cb.arcType == ArcNotPresent || cb.arcType == ArcPending {
continue
}
if f == cb.Label() {
return true
}
}
if !matchPattern(ctx, c.Expr, f) {
ctx.notAllowedError(c.src, ca.src)
return false
}
return true
}

func (ctx *OpContext) addPositions(c Conjunct) {
if x, ok := c.x.(*ConjunctGroup); ok {
for _, c := range *x {
Expand Down

0 comments on commit 6916b5b

Please sign in to comment.