runtime: preserve type metadata in assertion panics - #2252
Open
cpunion wants to merge 2 commits into
Open
Conversation
LLGo baseline benchmarks
Program measurements
Core language and compiler benchmarks
No main baseline exists yet; all metrics are marked Warning
|
Contributor
There was a problem hiding this comment.
Review summary
Reviewed the type-assertion panic changes against the PR's actual diff (head 959307217 vs its parent). Four review passes (code quality, performance, security, docs) were run; findings that turned out to target unrelated code already on main (the RewriteMainPrefix removal) were discarded as out of scope for this PR.
No blocking issues found. This is a clean, well-scoped change.
What the change does and why it holds up:
runtime/internal/runtime/errors.go:PanicTypeAssertnow takes(source, concrete, asserted *_type, missingMethod)and constructs aTypeAssertionError, delegating all message formatting to the existingError()method instead of building strings inline. The dead helperssameTypeAssertName/typeNameSuffixare removed with no remaining references (hasPrefixis still used elsewhere).Error()already distinguishes "different packages" vs "different scopes" and handles the nil-concrete and missing-method cases, so behavior is preserved and made more gc-accurate.ssa/interface.go: the failure branch now passes the source interface's abi type — real pointer for non-empty interfaces (NumMethods() > 0),nilfor empty interfaces. Thesourcecomputation is emitted only into the panic block (blks[1]), so the successful-assertion hot path is unaffected; the singleTypeAssertionErrorheap allocation is confined to the cold, terminal panic path.- The regenerated FileCheck goldens are consistent with the new call shape:
anyassertions emitptr nullfor source, non-empty-interface assertions emit a real type pointer (e.g.cl/_testrt/any/in.go,cl/_testgo/tpinst/main.go). test/go/interface_type_assert_panic_test.go:TestGenericNilInterfaceAssertMentionsSourceTypecorrectly exercises the new source-type-in-message behavior for a nil non-empty interface.
Non-blocking observations (no action required):
TypeAssertionError.Error()dereferencese.asserted.String()without a nil guard, unlike the_interface/concretefields. This line is pre-existing (not changed by this PR) and every construction site (ssa/interface.go,z_face.go) passes a non-nilasserted, so it is safe today — noting only as a latent invariant.- The removed
test/goroot/xfail.yamlentries (typeparam/mdempsky/16.go,fixedbugs/issue18911.go,interface/embed3.go) match what this change fixes. Theembed3.gocase depends on_type.String()/pkgpath()output, which this PR does not modify; worth confirming those three goroot cases are green in CI before merge.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type assertion failures currently lower the asserted type to a string before calling the runtime. That loses package identity, the source interface type, and instantiated generic type information.
Pass ABI type descriptors for the source interface and asserted type instead, and construct the existing Go-compatible
TypeAssertionErrordirectly. Empty source interfaces remain nil so ordinary interface-conversion messages keep their Go spelling.This removes the xfails for:
fixedbugs/issue18911.gointerface/embed3.gotypeparam/mdempsky/16.gofixedbugs/issue26094.goThe scoped same-name type regression is covered in
TestInterfaceAssertRejectsSameNameTypesFromDifferentScopes; it checks both local-to-local and local-to-package assertions.Validation:
go test ./ssa ./test/goroot -count=1 -timeout=15mfixedbugs/issue26094.gowith empty xfail on all three Go versions on darwin/arm64