Closed mneverov closed 4 days ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
As the error indicates, this is because you declared type A[P int]
, which means the generic type can only be int
, hence string
is not allowed.
Changing your variable to be var ta A[int]
, or changing your type declarations to be type T[P any]
and type A[P any]
resolves it.
as mentioned above, type aliases don't allow you to relax type constraints.
Think it should be reopened. OP seems to wonder why it compiles. Seems like a bug.
(made the same mistake initially :)
as mentioned above, type aliases don't allow you to relax type constraints.
I'm not relaxing the constraint with the alias. Declaring an alias with the same constraint as the initial type constraint eliminates it. The code compiles, but should error out FMPV.
CC @griesemer @findleyr
This does look like a bug. The type alias permits type arguments that are not permitted by the constraint.
It appears that we're not checking the constraints for alias type parameters at all. Simpler reproducer:
package p
type A[P int] = struct{}
var _ A[string] // no error reported here
Temp. work-around: write correct code...
It appears that we're not checking the constraints for alias type parameters at all. Simpler reproducer:
package p type A[P int] = struct{} var _ A[string] // no error reported here
Temp. work-around: write correct code...
It seems to me that when checking var type, we stop immediately as long as we see typeparam.
@cuonglm This is unrelated to var types. It also happens if we declare another type that depends on A
. The problem is that Checker.instantiatedType
doesn't verify the instantiation if the instantiated type is an alias.
Change https://go.dev/cl/615275 mentions this issue: go/types, types2: check that alias type arguments satisfy constraints
Go version
go version devel go1.24-c208b91395 Fri Sep 20 13:54:44 2024 +0000 X:aliastypeparams linux/amd64
Output of
go env
in your module/workspace:What did you do?
see in playground
What did you see happen?
The output is
main.T[string]{f:""}
What did you expect to see?
error
string does not satisfy int (string missing in int)