1
0
mirror of https://github.com/golang/go synced 2024-09-29 12:14:28 -06:00

Revert "go/types, types2: remove unnecessary tests for x.typ == Typ[Invalid]"

This reverts commit 4c49d52439.

Reason for revert: it is trickier than expected to enforce an invariant that x.typ == Typ[Invalid] => x.mode == invalid. For example, builtins have invalid type until their call is evaluated.

I think it is better to keep this defensive code for now. My bad for suggesting this strictness. I will send a follow-up CL with a test that exercises the panic discovered inside Google, and a bit more commentary about what 'invalid' means in both contexts.

Fixes #59603

Change-Id: If291f7268e7ef7ae6cd9bb861bb9af349a729cb2
Reviewed-on: https://go-review.googlesource.com/c/go/+/484375
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Findley 2023-04-13 13:55:42 +00:00
parent 3f747d09e4
commit 0742e6dbc0
2 changed files with 12 additions and 4 deletions

View File

@ -99,7 +99,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
}
func (check *Checker) initConst(lhs *Const, x *operand) {
if x.mode == invalid || lhs.typ == Typ[Invalid] {
if x.mode == invalid || x.typ == Typ[Invalid] || lhs.typ == Typ[Invalid] {
if lhs.typ == nil {
lhs.typ = Typ[Invalid]
}
@ -133,7 +133,7 @@ func (check *Checker) initConst(lhs *Const, x *operand) {
// If lhs doesn't have a type yet, it is given the type of x,
// or Typ[Invalid] in case of an error.
func (check *Checker) initVar(lhs *Var, x *operand, context string) {
if x.mode == invalid || lhs.typ == Typ[Invalid] {
if x.mode == invalid || x.typ == Typ[Invalid] || lhs.typ == Typ[Invalid] {
if lhs.typ == nil {
lhs.typ = Typ[Invalid]
}
@ -195,6 +195,10 @@ func (check *Checker) lhsVar(lhs syntax.Expr) Type {
v.used = v_used // restore v.used
}
if x.mode == invalid || x.typ == Typ[Invalid] {
return Typ[Invalid]
}
// spec: "Each left-hand side operand must be addressable, a map index
// expression, or the blank identifier. Operands may be parenthesized."
switch x.mode {

View File

@ -97,7 +97,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
}
func (check *Checker) initConst(lhs *Const, x *operand) {
if x.mode == invalid || lhs.typ == Typ[Invalid] {
if x.mode == invalid || x.typ == Typ[Invalid] || lhs.typ == Typ[Invalid] {
if lhs.typ == nil {
lhs.typ = Typ[Invalid]
}
@ -131,7 +131,7 @@ func (check *Checker) initConst(lhs *Const, x *operand) {
// If lhs doesn't have a type yet, it is given the type of x,
// or Typ[Invalid] in case of an error.
func (check *Checker) initVar(lhs *Var, x *operand, context string) {
if x.mode == invalid || lhs.typ == Typ[Invalid] {
if x.mode == invalid || x.typ == Typ[Invalid] || lhs.typ == Typ[Invalid] {
if lhs.typ == nil {
lhs.typ = Typ[Invalid]
}
@ -193,6 +193,10 @@ func (check *Checker) lhsVar(lhs ast.Expr) Type {
v.used = v_used // restore v.used
}
if x.mode == invalid || x.typ == Typ[Invalid] {
return Typ[Invalid]
}
// spec: "Each left-hand side operand must be addressable, a map index
// expression, or the blank identifier. Operands may be parenthesized."
switch x.mode {