this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/ast,cue/ast/astutil: add Predeclared sentinel for builtin references

Add ast.Predeclared, a sentinel node that can be used as ast.Ident.Node
to mark a reference to a predeclared identifier like "self" or "int".

When Sanitize encounters a shadowed identifier with this sentinel, it
renames it to the "__"-prefixed form (e.g. "__self") which cannot be
shadowed, rather than introducing a let alias.

For #4151.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iccc6cd07da85898230891bd135f7dc964fe460af
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1231144
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+25 -2
+16
cue/ast/ast.go
··· 450 450 expr 451 451 } 452 452 453 + // Predeclared is a sentinel [Node] that can be used as [Ident.Node] 454 + // for an identifier referring to a predeclared name such as "self" or "int". 455 + // 456 + // When [cuelang.org/go/cue/ast/astutil.Sanitize] encounters an identifier 457 + // whose Node is this sentinel and the name is shadowed in scope, it renames 458 + // the identifier to its "__"-prefixed form (e.g. "__self") to avoid the shadow. 459 + var Predeclared Node = &predeclaredNode{} 460 + 461 + type predeclaredNode struct { 462 + comments 463 + } 464 + 465 + func (n *predeclaredNode) Pos() token.Pos { return token.NoPos } 466 + func (n *predeclaredNode) pos() *token.Pos { return nil } 467 + func (n *predeclaredNode) End() token.Pos { return token.NoPos } 468 + 453 469 // A BasicLit node represents a literal of basic type. 454 470 type BasicLit struct { 455 471 ValuePos token.Pos // literal position
+9 -2
cue/ast/astutil/sanitize.go
··· 28 28 // - handle comprehensions 29 29 // - change field from foo to "foo" if it isn't referenced, rather than 30 30 // relying on introducing a unique alias. 31 - // - change a predeclared identifier reference to use the __ident form, 32 - // instead of introducing an alias. 33 31 34 32 // Sanitize rewrites File f in place to be well-formed after automated 35 33 // construction of an AST. ··· 256 254 257 255 if node.node == n.Node { 258 256 return true 257 + } 258 + 259 + // A predeclared reference (e.g. "self") is shadowed by a local 260 + // declaration. Use the "__"-prefixed form to avoid the shadow. 261 + if n.Node == ast.Predeclared { 262 + n.Name = "__" + n.Name 263 + n.Node = nil 264 + n.Scope = nil 265 + return false 259 266 } 260 267 261 268 // n.Node != node and are both not nil and n.Node is not an ImportSpec.