1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:00:08 -07:00

cmd/compile/internal/types2: systematic detection of missing instantiation

When type-checking expressions, detection of uninstantiated generic
functions and types was somewhat ad-hoc.

Add an extra parameter "allowGenerics" to rawExpr. If not set, the
result operand cannot be generic.

The only place where rawExpr is called with allowGenerics != false
is from exprOrType, which passes an allowGenerics parameter through.

The only place where exprOrType is called with allowGenerics == true
is when handling index expressions and calls. Make sure that we only
accept generic operands where expected, and check the other branches.

As a result, a recently added varType call (CL 345970) can be removed
again.

This also fixes a bug where an error for a conversion to generic
type was reported after the conversion (i.e., with the converted
value, rather than the generic type). Added a test case for that.

For #48048.

Change-Id: I8576326f5fcfb58d78b3ce8572068aa32e66c568
Reviewed-on: https://go-review.googlesource.com/c/go/+/346471
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-08-31 17:43:18 -07:00
parent 0bfd6fcea6
commit 2872496ba5
7 changed files with 66 additions and 21 deletions

View File

@ -763,7 +763,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
var t operand var t operand
x1 := x x1 := x
for _, arg := range call.ArgList { for _, arg := range call.ArgList {
check.rawExpr(x1, arg, nil) // permit trace for types, e.g.: new(trace(T)) check.rawExpr(x1, arg, nil, false) // permit trace for types, e.g.: new(trace(T))
check.dump("%v: %s", posFor(x1), x1) check.dump("%v: %s", posFor(x1), x1)
x1 = &t // use incoming x only for first argument x1 = &t // use incoming x only for first argument
} }

View File

@ -83,8 +83,9 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
x.expr = iexpr x.expr = iexpr
check.record(x) check.record(x)
} else { } else {
check.exprOrType(x, call.Fun) check.exprOrType(x, call.Fun, true)
} }
// x.typ may be generic
switch x.mode { switch x.mode {
case invalid: case invalid:
@ -94,6 +95,10 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
case typexpr: case typexpr:
// conversion // conversion
check.nonGeneric(x)
if x.mode == invalid {
return conversion
}
T := x.typ T := x.typ
x.mode = invalid x.mode = invalid
switch n := len(call.ArgList); n { switch n := len(call.ArgList); n {
@ -122,6 +127,7 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
return conversion return conversion
case builtin: case builtin:
// no need to check for non-genericity here
id := x.id id := x.id
if !check.builtin(x, call, id) { if !check.builtin(x, call, id) {
x.mode = invalid x.mode = invalid
@ -135,6 +141,7 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
} }
// ordinary function/method call // ordinary function/method call
// signature may be generic
cgocall := x.mode == cgofunc cgocall := x.mode == cgofunc
sig := asSignature(x.typ) sig := asSignature(x.typ)
@ -474,15 +481,11 @@ func (check *Checker) selector(x *operand, e *syntax.SelectorExpr) {
} }
} }
check.exprOrType(x, e.X) check.exprOrType(x, e.X, false)
if x.mode == invalid { if x.mode == invalid {
goto Error goto Error
} }
if x.mode == typexpr {
x.typ = check.varType(e.X)
}
obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel) obj, index, indirect = LookupFieldOrMethod(x.typ, x.mode == variable, check.pkg, sel)
if obj == nil { if obj == nil {
switch { switch {
@ -683,7 +686,7 @@ func (check *Checker) use(arg ...syntax.Expr) {
check.use(l.ElemList...) check.use(l.ElemList...)
continue continue
} }
check.rawExpr(&x, e, nil) check.rawExpr(&x, e, nil, false)
} }
} }
@ -714,7 +717,7 @@ func (check *Checker) useLHS(arg ...syntax.Expr) {
} }
} }
} }
check.rawExpr(&x, e, nil) check.rawExpr(&x, e, nil, false)
if v != nil { if v != nil {
v.used = v_used // restore v.used v.used = v_used // restore v.used
} }

View File

@ -1085,8 +1085,10 @@ const (
// rawExpr typechecks expression e and initializes x with the expression // rawExpr typechecks expression e and initializes x with the expression
// value or type. If an error occurred, x.mode is set to invalid. // value or type. If an error occurred, x.mode is set to invalid.
// If hint != nil, it is the type of a composite literal element. // If hint != nil, it is the type of a composite literal element.
// If allowGeneric is set, the operand type may be an uninstantiated
// parameterized type or function value.
// //
func (check *Checker) rawExpr(x *operand, e syntax.Expr, hint Type) exprKind { func (check *Checker) rawExpr(x *operand, e syntax.Expr, hint Type, allowGeneric bool) exprKind {
if check.conf.Trace { if check.conf.Trace {
check.trace(e.Pos(), "expr %s", e) check.trace(e.Pos(), "expr %s", e)
check.indent++ check.indent++
@ -1097,11 +1099,40 @@ func (check *Checker) rawExpr(x *operand, e syntax.Expr, hint Type) exprKind {
} }
kind := check.exprInternal(x, e, hint) kind := check.exprInternal(x, e, hint)
if !allowGeneric {
check.nonGeneric(x)
}
check.record(x) check.record(x)
return kind return kind
} }
// If x is a generic function or type, nonGeneric reports an error and invalidates x.mode and x.typ.
// Otherwise it leaves x alone.
func (check *Checker) nonGeneric(x *operand) {
if x.mode == invalid || x.mode == novalue {
return
}
var what string
switch t := x.typ.(type) {
case *Named:
if isGeneric(t) {
what = "type"
}
case *Signature:
if t.tparams != nil {
what = "function"
}
}
if what != "" {
check.errorf(x.expr, "cannot use generic %s %s without instantiation", what, x.expr)
x.mode = invalid
x.typ = Typ[Invalid]
}
}
// exprInternal contains the core of type checking of expressions. // exprInternal contains the core of type checking of expressions.
// Must only be called by rawExpr. // Must only be called by rawExpr.
// //
@ -1386,7 +1417,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
x.typ = typ x.typ = typ
case *syntax.ParenExpr: case *syntax.ParenExpr:
kind := check.rawExpr(x, e.X, nil) kind := check.rawExpr(x, e.X, nil, false)
x.expr = e x.expr = e
return kind return kind
@ -1468,7 +1499,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
// unary expression // unary expression
if e.Op == syntax.Mul { if e.Op == syntax.Mul {
// pointer indirection // pointer indirection
check.exprOrType(x, e.X) check.exprOrType(x, e.X, false)
switch x.mode { switch x.mode {
case invalid: case invalid:
goto Error goto Error
@ -1595,14 +1626,14 @@ func (check *Checker) typeAssertion(pos syntax.Pos, x *operand, xtyp *Interface,
// If an error occurred, x.mode is set to invalid. // If an error occurred, x.mode is set to invalid.
// //
func (check *Checker) expr(x *operand, e syntax.Expr) { func (check *Checker) expr(x *operand, e syntax.Expr) {
check.rawExpr(x, e, nil) check.rawExpr(x, e, nil, false)
check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr) check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
check.singleValue(x) check.singleValue(x)
} }
// multiExpr is like expr but the result may also be a multi-value. // multiExpr is like expr but the result may also be a multi-value.
func (check *Checker) multiExpr(x *operand, e syntax.Expr) { func (check *Checker) multiExpr(x *operand, e syntax.Expr) {
check.rawExpr(x, e, nil) check.rawExpr(x, e, nil, false)
check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr) check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
} }
@ -1612,16 +1643,18 @@ func (check *Checker) multiExpr(x *operand, e syntax.Expr) {
// //
func (check *Checker) exprWithHint(x *operand, e syntax.Expr, hint Type) { func (check *Checker) exprWithHint(x *operand, e syntax.Expr, hint Type) {
assert(hint != nil) assert(hint != nil)
check.rawExpr(x, e, hint) check.rawExpr(x, e, hint, false)
check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr) check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
check.singleValue(x) check.singleValue(x)
} }
// exprOrType typechecks expression or type e and initializes x with the expression value or type. // exprOrType typechecks expression or type e and initializes x with the expression value or type.
// If allowGeneric is set, the operand type may be an uninstantiated parameterized type or function
// value.
// If an error occurred, x.mode is set to invalid. // If an error occurred, x.mode is set to invalid.
// //
func (check *Checker) exprOrType(x *operand, e syntax.Expr) { func (check *Checker) exprOrType(x *operand, e syntax.Expr, allowGeneric bool) {
check.rawExpr(x, e, nil) check.rawExpr(x, e, nil, allowGeneric)
check.exclude(x, 1<<novalue) check.exclude(x, 1<<novalue)
check.singleValue(x) check.singleValue(x)
} }

View File

@ -15,7 +15,8 @@ import (
// In that case x represents the uninstantiated function value and // In that case x represents the uninstantiated function value and
// it is the caller's responsibility to instantiate the function. // it is the caller's responsibility to instantiate the function.
func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst bool) { func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst bool) {
check.exprOrType(x, e.X) check.exprOrType(x, e.X, true)
// x may be generic
switch x.mode { switch x.mode {
case invalid: case invalid:
@ -25,6 +26,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
case typexpr: case typexpr:
// type instantiation // type instantiation
x.mode = invalid x.mode = invalid
// TODO(gri) here we re-evaluate e.X - try to avoid this
x.typ = check.varType(e) x.typ = check.varType(e)
if x.typ != Typ[Invalid] { if x.typ != Typ[Invalid] {
x.mode = typexpr x.mode = typexpr
@ -38,6 +40,12 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
} }
} }
// x should not be generic at this point, but be safe and check
check.nonGeneric(x)
if x.mode == invalid {
return false
}
// ordinary index expression // ordinary index expression
valid := false valid := false
length := int64(-1) // valid if >= 0 length := int64(-1) // valid if >= 0

View File

@ -170,7 +170,7 @@ func (check *Checker) closeScope() {
func (check *Checker) suspendedCall(keyword string, call *syntax.CallExpr) { func (check *Checker) suspendedCall(keyword string, call *syntax.CallExpr) {
var x operand var x operand
var msg string var msg string
switch check.rawExpr(&x, call, nil) { switch check.rawExpr(&x, call, nil, false) {
case conversion: case conversion:
msg = "requires function call, not conversion" msg = "requires function call, not conversion"
case expression: case expression:
@ -386,7 +386,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
// function and method calls and receive operations can appear // function and method calls and receive operations can appear
// in statement context. Such statements may be parenthesized." // in statement context. Such statements may be parenthesized."
var x operand var x operand
kind := check.rawExpr(&x, s.X, nil) kind := check.rawExpr(&x, s.X, nil, false)
var msg string var msg string
switch x.mode { switch x.mode {
default: default:

View File

@ -102,6 +102,7 @@ func _() {
// Generic types cannot be used without instantiation. // Generic types cannot be used without instantiation.
var _ T // ERROR cannot use generic type T var _ T // ERROR cannot use generic type T
var _ = T /* ERROR cannot use generic type T */ (0)
// In type context, generic (parameterized) types cannot be parenthesized before // In type context, generic (parameterized) types cannot be parenthesized before
// being instantiated. See also NOTES entry from 12/4/2019. // being instantiated. See also NOTES entry from 12/4/2019.

View File

@ -85,7 +85,7 @@ var x T25 /* ERROR without instantiation */ .m1
// crash 26 // crash 26
type T26 = interface{ F26[ /* ERROR cannot have type parameters */ Z any]() } type T26 = interface{ F26[ /* ERROR cannot have type parameters */ Z any]() }
func F26[Z any]() T26 { return F26 /* ERROR without instantiation */ /* ERROR missing method */ [] /* ERROR operand */ } func F26[Z any]() T26 { return F26 /* ERROR without instantiation */ [] /* ERROR operand */ }
// crash 27 // crash 27
func e27[T any]() interface{ x27 /* ERROR not a type */ } { panic(0) } func e27[T any]() interface{ x27 /* ERROR not a type */ } { panic(0) }