this repo has no description
0
fork

Configure Feed

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

tools/fix: use ast.Predeclared to avoid shadowing in value alias conversion

When "cue fix --exp=aliasv2" converts old-style value aliases
(X={...}) to "let X = self", the generated code breaks if "self" is
already in scope as a field, let clause, or alias.

Mark the generated "self" identifier with ast.Predeclared so that
Sanitize automatically renames it to "__self" when shadowed.

Fixes #4151.

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

+10 -6
+5 -1
tools/fix/fix.go
··· 486 486 } 487 487 488 488 // Create a let clause: let X = self 489 + // Mark the "self" identifier as a predeclared reference so that 490 + // Sanitize will rename it to "__self" if shadowed in scope. 491 + selfIdent := ast.NewIdent("self") 492 + selfIdent.Node = ast.Predeclared 489 493 letClause := &ast.LetClause{ 490 494 Ident: alias.Ident, 491 - Expr: ast.NewIdent("self"), 495 + Expr: selfIdent, 492 496 } 493 497 494 498 // Insert the let clause at the beginning of the struct
+5 -5
tools/fix/testdata/aliasv2.txtar
··· 348 348 349 349 // Field named self in same struct 350 350 samestruct: { 351 - let X = self 351 + let X = __self 352 352 i: X.self 353 353 self: 42 354 354 } ··· 357 357 enclosingfield: { 358 358 self: 42 359 359 inner: { 360 - let X = self 360 + let X = __self 361 361 i: X.a 362 362 a: 1 363 363 } ··· 367 367 enclosinglet: { 368 368 let self = {z: 99} 369 369 inner: { 370 - let X = self 370 + let X = __self 371 371 i: X.a 372 372 a: 1 373 373 j: self.z ··· 381 381 } 382 382 baz: self.data 383 383 foo: { 384 - let X = self 384 + let X = __self 385 385 i: X.a 386 386 a: 1 387 387 } ··· 395 395 baz: self.data 396 396 inner: { 397 397 foo: { 398 - let X = self 398 + let X = __self 399 399 i: X.a 400 400 a: 1 401 401 }