Skip to content

Go: fix DefinedType.getBaseType #19654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

owen-mc
Copy link
Contributor

@owen-mc owen-mc commented Jun 3, 2025

Consider this situation:

type T1 = T2

type T2 struct {}

Previously, DefinedType.getBaseType just gave the underlying type, so calling it on T1 would give struct {}. This PR fixes it so that DefinedType.getBaseType gives the right hand side of the type declaration, and hence calling it on T1 gives T2.

Note that this requires us to have extracted the declaration of the type, so it is only available for types declared in the project being analyzed. It is not defined for types defined in external packages. Since this information is not stored in the information that we already extract for external packages, I cannot see any way to improve this situation except for expanding what we look at from external packages.

I have also fixed a test which was the only use of DefinedType.getBaseType outside of the definition of DefinedType, and added a proper test for DefinedType.getBaseType.

@Copilot Copilot AI review requested due to automatic review settings June 3, 2025 14:00
@owen-mc owen-mc requested a review from a team as a code owner June 3, 2025 14:00
@github-actions github-actions bot added the Go label Jun 3, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR changes DefinedType.getBaseType to return the alias’s right‐hand side (the declared type) rather than its underlying type, updates related methods to maintain correct semantics, and refreshes library tests accordingly.

  • Reimplement getBaseType to read from the declaration AST so aliases return their RHS.
  • Update getMethod and getUnderlyingType to use the new separation between base and underlying types.
  • Revise existing method‐resolution tests and add new tests for getBaseType.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
go/ql/test/library-tests/semmle/go/Types/MethodTypes.ql Use Type.hasMethod and add a declaration existence check
go/ql/test/library-tests/semmle/go/Types/MethodTypes.expected Adjust expected methods list after alias semantics change
go/ql/test/library-tests/semmle/go/Types/DefinedType_getBaseType.ql New test querying DefinedType.getBaseType()
go/ql/test/library-tests/semmle/go/Types/DefinedType_getBaseType.expected Expected output for the new getBaseType test
go/ql/lib/semmle/go/Types.qll Change getBaseType, getUnderlyingType, and related logic
Comments suppressed due to low confidence (1)

go/ql/test/library-tests/semmle/go/Types/DefinedType_getBaseType.ql:4

  • Consider adding a test case to verify that getBaseType returns no result for types declared in external packages, ensuring the new behavior is validated against external definitions.
where tp = dt.getBaseType()

* Gets the type which this type is defined to be, if available.
*
* Note that this is only defined for types declared in the project being
* analyzed. It will not be defined for type declared in external packages.
Copy link
Preview

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the plural form here: change "type declared in external packages" to "types declared in external packages" for grammatical consistency.

Suggested change
* analyzed. It will not be defined for type declared in external packages.
* analyzed. It will not be defined for types declared in external packages.

Copilot uses AI. Check for mistakes.

@owen-mc owen-mc force-pushed the go/fix-definedtype-getbasetype branch from 9535baf to b2f310c Compare June 3, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant