Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disjunction with default value behind two definitions doesn't work as expected #2420

Closed
uhthomas opened this issue May 25, 2023 · 3 comments
Closed
Assignees
Labels
Documentation NeedsInvestigation roadmap/errors Related to improving error messages.

Comments

@uhthomas
Copy link
Contributor

What version of CUE are you using (cue version)?

$ cue version
0.5.0

Also happens when built from HEAD.

Does this issue reproduce with the latest stable release?

Yes.

What did you do?

#Main: {
    name: string
    objects: {
        for v in ["some key"] {
            "\(v)": #Other & { name: name }
        }
    }
}

#Other: {
    name: string | *"unset"
    value: "Hi, \(name)!"
}

out: #Main & { name: "some name" }

https://cuelang.org/play/?id=Sgruw0xqHao#cue@export@json

What did you expect to see?

{
    "out": {
        "name": "some name",
        "objects": {
            "some key": {
                "name": "unset",
                "value": "Hi, some name!"
            }
        }
    }
}

What did you see instead?

{
    "out": {
        "name": "some name",
        "objects": {
            "some key": {
                "name": "unset",
                "value": "Hi, unset!"
            }
        }
    }
}

Strangely, this issue is resolved if an alias is used...

#Main: {
    name: string
    objects: {
        for v in ["some key"] {
            let _name = name
            "\(v)": #Other & { name: _name }
        }
    }
}

#Other: {
    name: string | *"unset"
    value: "Hi, \(name)!"
}

out: #Main & { name: "some name" }

https://cuelang.org/play/?id=J3sMaBR5eJ2#cue@export@json

@uhthomas uhthomas added NeedsInvestigation Triage Requires triage/attention labels May 25, 2023
@mvdan
Copy link
Member

mvdan commented May 31, 2023

Smaller repro: https://cuelang.org/play/?id=yFTOsLExm5T#cue@export@json

$ cat f.cue
#D1 & {in: "explicit"}

#D1: {
	in:  string
	out: #D2 & {in: in}
}

#D2: {
	in: string | *"default"
}
$ cue export f.cue
{
    "in": "explicit",
    "out": {
        "in": "default"
    }
}

I would expect out.in to be "explicit", not "default". It starts working if you simplify the definitions a bit, such as making the second definition a string rather than a struct: https://cuelang.org/play/?id=wp1TUDMikIK#cue@export@json

At least this is not a recent regression; I see the same behavior with v0.4.3.

@mvdan mvdan removed the Triage Requires triage/attention label May 31, 2023
@mvdan mvdan changed the title Values aren't passed to list comprehensions correctly disjunction with default value behind two definitions doesn't work as expected May 31, 2023
@myitcv myitcv added zGarden Documentation roadmap/errors Related to improving error messages. and removed zGarden labels Jun 13, 2023
@mvdan mvdan self-assigned this Jun 13, 2023
@mvdan mvdan added the Triage Requires triage/attention label Jun 13, 2023
@mvdan
Copy link
Member

mvdan commented Jun 19, 2023

I completely missed that you were using #Other & {name: name}. Note that the second name refers to the name: field in the same line, not the original one near the top at #Main.name. Similar to Go, you are shadowing the original name declaration with another one.

You can work around this with a let field, for example: https://cuelang.org/play/?id=dibAxjMilmf#cue@export@json

An alternative would be to try to avoid repeating field names in types which will be nested like this, to prevent issues with shadowing.

#466 tracks doing something to warn about this kind of shadowing. It almost never makes sense to declare a field like foo: foo, especially not if the parent scope already has a foo that the user likely meant to reference.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2023
@mvdan mvdan removed the Triage Requires triage/attention label Jun 19, 2023
@uhthomas
Copy link
Contributor Author

I think you're right, thank you for investigating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation roadmap/errors Related to improving error messages.
Projects
None yet
Development

No branches or pull requests

3 participants