Skip to content

Unify nested maps #778

@purpleidea

Description

@purpleidea

Versions:

  • mgmt version (eg: mgmt --version):

Current git master of: 43bd847

  • golang version (eg: go version):

go version go1.22.7 linux/amd64

Description:

I'd expect the following code to unify automatically, but it does not. If we specify the type (as shown) then it works.

$bigmap = {
	"key1" => {
		"foo" => "thing1a",
		"bar" => "thing1b",
	},
	"key2" => {
		"foo" => "thing2a",	# pull out this!
		"bar" => "thing2b",
	},
}

$key = "key2"

#$inner map{str: str} = $bigmap[$key] # does unify with the type hint!
$inner = $bigmap[$key] # doesn't unify!

print "debug" {
	msg => $inner["foo"],
}

You can easily run this with:

./mgmt run --tmp-prefix lang unify-bug-or-not.mcl

I also tried applying this patch in case, but it did not change the result:

diff --git lang/ast/structs.go lang/ast/structs.go
index a8b844bc6..f0d80447e 100644
--- lang/ast/structs.go
+++ lang/ast/structs.go
@@ -7689,6 +7689,15 @@ func (obj *ExprFunc) Infer() (*types.Type, []*interfaces.UnificationInvariant, e
                }
                invariants = append(invariants, invars...)
 
+               // XXX >>>
+               invarFoo := &interfaces.UnificationInvariant{
+                       Expr:   obj.params[i],
+                       Expect: typArg,
+                       Actual: typArg,
+               }
+               invariants = append(invariants, invarFoo)
+               // <<< XXX
+
                m[arg.Name] = typArg
                ord = append(ord, arg.Name)
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions