Skip to content

fix: handle untyped shift expressions in SSA build#1531

Merged
xushiwei merged 1 commit intogoplus:mainfrom
cpunion:fix/untyped
Jan 7, 2026
Merged

fix: handle untyped shift expressions in SSA build#1531
xushiwei merged 1 commit intogoplus:mainfrom
cpunion:fix/untyped

Conversation

@cpunion
Copy link
Collaborator

@cpunion cpunion commented Jan 5, 2026

Summary

This PR fixes the SSA sanity check failure on nested shift expressions like:

value << (1 << bytes[3])

Root Cause

The issue is that go/types returns untyped int for 1 << bytes[3], but go/ssa sanity check disallows untyped instruction results, causing a panic:

Error: instruction has 'untyped' result: t2 = 1:untyped int << t1 : untyped int
panic: SanityCheck failed

This was reported upstream: golang/go#77067

Solution

Added fixUntypedShiftTypes() function to convert untyped shift types to their default type (using types.Default()) before calling prog.CreatePackage. This applies the conversion that the Go spec requires:

"If the left operand of a non-constant shift expression is an untyped constant, it is first implicitly converted to the type it would assume if the shift expression were replaced by its left operand alone."

Changes

  • internal/build/build.go: Added fixUntypedShiftTypes() workaround
  • test/go/shift_untyped_test.go: Added test cases

Testing

  • go test ./test/go passes
  • go run ./cmd/llgo test ./test/go passes

Fixes #1530

@gemini-code-assist
Copy link

Summary of Changes

Hello @cpunion, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue where nested shift expressions involving untyped constants could cause a panic during the SSA build process. The fix introduces a preprocessing step to correctly type these expressions according to Go's specification, ensuring the SSA generator receives properly typed inputs and preventing runtime failures.

Highlights

  • SSA Sanity Check Fix: Addresses a panic in go/ssa that occurred with nested shift expressions where go/types returned an "untyped int" for the inner shift, violating SSA's sanity checks.
  • Untyped Shift Type Conversion: Implements fixUntypedShiftTypes() to explicitly convert the type of untyped shift expression results to their default type (e.g., int) before the SSA build process, aligning with Go's implicit conversion rules.
  • New Test Cases: Adds test/go/shift_untyped_test.go with specific test cases to reproduce and verify the fix for nested shift expressions, including edge cases.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a workaround for an upstream bug in go/types that causes panics in the SSA build for certain nested shift expressions. The fix involves iterating through expression types and correcting untyped shift expression results to their default types. A comprehensive set of tests has been added to cover the buggy behavior and ensure the fix is effective. The implementation is correct, but I've suggested a small refactoring to improve readability by reducing nested conditions.

@xgopilot
Copy link
Contributor

xgopilot bot commented Jan 5, 2026

Code Review Summary

Excellent fix for the SSA sanity check failure! The implementation is clean, focused, and includes comprehensive test coverage.

Key Strengths:

  • Clear problem documentation with upstream issue reference
  • Well-structured test cases covering the original bug and edge cases
  • Minimal, targeted workaround approach

Critical Issue:

  • Map modification during iteration in fixUntypedShiftTypes - should use two-pass approach

Documentation Improvements:

  • Add Go spec quote to function documentation
  • Fix "pattern2" reference in test comments

No security or performance concerns identified.

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.04%. Comparing base (bba9538) to head (f88f80d).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1531   +/-   ##
=======================================
  Coverage   91.04%   91.04%           
=======================================
  Files          45       45           
  Lines       11999    11999           
=======================================
  Hits        10924    10924           
  Misses        899      899           
  Partials      176      176           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This fixes the SSA sanity check failure on nested shift expressions like:
  value << (1 << bytes[3])

The issue is that go/types returns 'untyped int' for '1 << bytes[3]',
but go/ssa sanity check disallows untyped instruction results.

According to the Go spec: 'If the left operand of a non-constant shift
expression is an untyped constant, it is first implicitly converted to
the type it would assume if the shift expression were replaced by its
left operand alone.'

Added fixUntypedShiftTypes() to convert untyped shift types to their
default type (using types.Default()) before calling prog.CreatePackage.
Uses two-pass approach to avoid map modification during iteration.

Fixes goplus#1530
See also: golang/go#77067

Co-Authored-By: Warp <[email protected]>
@xushiwei xushiwei merged commit a3db8d0 into goplus:main Jan 7, 2026
45 checks passed
@cpunion cpunion deleted the fix/untyped branch March 6, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSA sanity check fails on nested shift with untyped constant: value << (1 << byteVar)

2 participants