Skip to content

go/types, types2: remove discrepancy between type-checker and compiler error messages #55326

@griesemer

Description

@griesemer

For historic reasons, some errors are report with different messages by the compiler and type checkers. For instance, the compiler reports "undefined: x" while the type checkers report "undeclared name: x" for an unknown identifier x.

types2 has a configuration flag (Config.UseCompilerErrorMessages) that is set if types2 is used by the compiler; go/types has an internal constant useCompilerErrorMessages (set to false) for symmetry of code.

We want to remove the discrepancy and standardize on just one error message, and the eliminate the Config.UseCompilerErrorMessages (types2) and useCompilerErrorMessages (go/types) flags altogether.

At this point there are 16 cases where error messages differ:

  Source example Compiler go/types, types2
       
1 var _ = x undefined: x undeclared name: x
2 var o = math.Omega undefined: math.Omega Omega not declared by package math
3 type T struct{}; func (T) m() {} // twice T.m redeclared in this block method m already declared for type T struct{}
4 import "math" imported and not used: "math" "math" imported but not used
5 import foo "math" imported and not used: "math" as foo "math" imported but not used as foo
6 s = x cannot use x (variable of type int) as type string in assignment cannot use x (variable of type int) as string value in assignment
7 var a, b = f() assignment mismatch: 2 variables but f returns 3 values cannot initialize 2 variables with 3 values
8 a, b = f() assignment mismatch: 2 variables but f returns 3 values cannot assign 3 values to 2 variables
9 _ = int(s) cannot convert s (variable of type string) to type int cannot convert s (variable of type string) to int
10 type T struct{ T } invalid recursive type T illegal cycle in declaration of T
11 _ = 1.0 << s invalid operation: 1.0 << s (shift of type float64) invalid operation: shifted operand 1.0 (type float64) must be integer
12 _ = 1 == true invalid operation: 1 == true (mismatched types ...) invalid operation: cannot compare 1 == true (mismatched types ...)
13 type S struct{}; _ = S{f: 0} unknown field 'f' in struct literal of type S unknown field f in struct literal
14 func f() (int, int); var x = f() multiple-value f() (value of type (int, int)) in single-value context 2-valued f() (value of type (int, int)) where single value is expected
15 var a, b = b, a initialization loop for a initialization cycle for a
16 var x interface{ m() } = 0 cannot use 0 (constant of type int) as ...: ...(missing m method) cannot use 0 (constant of type int) as ...: ...(missing method m)

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions