Find a file
Robert Findley 47e34ca533 go/types, types2: ensure that named types never expand infinitely
During type-checking, newly created instances share a type checking
Context which de-duplicates identical instances. However, when
unexpanded types escape the type-checking pass or are created via calls
to Instantiate, they lack this shared context. As reported in #52728,
this may lead to infinitely many identical but distinct types that are
reachable via the API.

This CL introduces a new invariant that ensures we don't create such
infinitely expanding chains: instances created during expansion share a
context with the type that led to their creation. During expansion, the
expanding type passes its Context to any newly created instances.

This ensures that cycles will eventually terminate with a previously
seen instance. For example, if we have an instantiation chain
T1[P]->T2[P]->T3[P]->T1[P], by virtue of this Context passing the
expansion of T3[P] will find the instantiation T1[P].

In general, storing a Context in a Named type could lead to pinning
types in memory unnecessarily, but in this case the Context pins only
those types that are reachable from the original instance. This seems
like a reasonable compromise between lazy and eager expansion.

Our treatment of Context was a little haphazard: Checker.bestContext
made it easy to get a context at any point, but made it harder to reason
about which context is being used. To fix this, replace bestContext with
Checker.context, which returns the type-checking context and panics on a
nil receiver. Update all call-sites to verify that the Checker is
non-nil when context is called.

Also make it a panic to call subst with a nil context. Instead, update
subst to explicitly accept a local (=instance) context along with a
global context, and require that one of them is non-nil. Thread this
through to the call to Checker.instance, and handle context updating
there.

Fixes #52728

Change-Id: Ib7f26eb8c406290325bc3212fda25421a37a1e8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/404885
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
2022-06-06 15:42:19 +00:00
.github .github: remove duplicate security link 2022-01-07 17:55:09 +00:00
api io: revert: add an Err field to LimitedReader 2022-06-04 14:00:38 +00:00
doc doc: update Go memory model 2022-06-04 18:42:07 +00:00
lib/time lib/time, time/tzdata: update to 2022a 2022-05-31 08:53:53 +00:00
misc misc/cgo/testsanitizers: buffer the signal channel in TestTSAN/tsan11 2022-05-27 15:58:52 +00:00
src go/types, types2: ensure that named types never expand infinitely 2022-06-06 15:42:19 +00:00
test cmd/compile: fix wrong unsafe.Offsetof evaluation inside generic function 2022-05-31 14:58:09 +00:00
.gitattributes all: treat all files as binary, but check in .bat with CRLF 2020-06-08 15:31:43 +00:00
.gitignore internal/buildcfg: move build configuration out of cmd/internal/objabi 2021-04-16 19:20:53 +00:00
AUTHORS A+C: add Jinzhu Zhang 2022-05-08 17:28:26 +00:00
codereview.cfg codereview.cfg: add codereview.cfg for master branch 2021-02-19 18:44:53 +00:00
CONTRIBUTING.md all: restore changes from faulty merge/revert 2018-02-12 20:13:59 +00:00
CONTRIBUTORS A+C: add Jinzhu Zhang 2022-05-08 17:28:26 +00:00
LICENSE doc: revert copyright date to 2009 2016-06-01 22:40:04 +00:00
PATENTS LICENSE: separate, change PATENTS text 2010-12-06 16:31:59 -05:00
README.md README.md: update wiki link 2022-04-26 16:21:18 +00:00
SECURITY.md SECURITY.md: replace golang.org with go.dev 2022-04-26 19:59:47 +00:00

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.

Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.

Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.

Contributing

Go is the work of thousands of contributors. We appreciate your help!

To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.

Note that the Go project uses the issue tracker for bug reports and proposals only. See https://go.dev/wiki/Questions for a list of places to ask questions about the Go language.