golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.41k stars 17.7k forks source link

go/types: expose interface satisfaction constraints #8970

Open adonovan opened 10 years ago

adonovan commented 10 years ago
The type checker goes to very great lengths to be robust and precise even for ill-typed
inputs, and it is very hard to match this level of robustness in other passes over the
(typed) AST.  As a result, the refactor/lexical and refactor/satisfy packages, both of
which recompute information computed but not exposed by go/types, are not safe to run on
programs with type errors.

refactor/lexical records, for every referring identifier, the structure of the lexical
environment at that point, information that is necessary for refactoring tools and
impossible to derive from the Scope information currently exposed by the type checker
(which is rarely useful).

refactor/satisfies records the set of type pairs (x, y) such that x is assignable to
interface y, and if it were not assignable, the program would not compile.  This also is
necessary for refactoring tools.

I think the 'lexical' pass could be very cheaply and elegantly implemented into the
existing type checker, and I think it should be since it is so useful; 'satisfies' is 
trickier.
ianlancetaylor commented 10 years ago

Comment 1:

Labels changed: added repo-tools, release-none.

alandonovan commented 8 years ago

Update: the refactor/lexical half of this issue was fixed by https://go-review.googlesource.com/10800 (and refactor/lexical was deleted). The refactor/satisfies part remains.

griesemer commented 6 years ago

@alandonovan Please comment on this issue if you still think this is useful and explain what kind of API you'd suggest. Otherwise, feel free to close it. Thanks.

alandonovan commented 6 years ago

Here's a straw-man go/types API that would let us evaluate whether a small change to the implementation would eliminate the refactor/satisfies package:

We add a new field, Satisfies [][2]Type, to the Info struct. If this field is non-nil (even if empty), the client is assumed to want the extra information. Each time we call operand.assignableTo(x, T) where T is an non-empty interface type, x.typ is not identical to T, and the call returns true, we append a pair (x.typ, T) to the Satisfies slice.

The only change to assignableTo would be within the case documented as "T is an interface type and x implements T". A pointer to the Satisfies slice would be passed in, with nil meaning "don't record the information" as in the case where assignableTo is called from the public AssignableTo function.

I guess I should prototype this myself.

griesemer commented 6 years ago

@alandonovan Seems fine to me implementation-wise. But as described, this will only record pair combinations that appeared in the type-checked code. Is that sufficient?

alandonovan commented 6 years ago

Yes, that's sufficient. Users can already compute assignability for arbitrary pairs of types; what's missing is knowing which types' assignability mattered to a particular package.