Skip to content

fix(typing): resolve invalid Self to Any during annotation solving to prevent internal-error#3014

Closed
Arths17 wants to merge 1 commit intofacebook:mainfrom
Arths17:Issue-#3008
Closed

fix(typing): resolve invalid Self to Any during annotation solving to prevent internal-error#3014
Arths17 wants to merge 1 commit intofacebook:mainfrom
Arths17:Issue-#3008

Conversation

@Arths17
Copy link
Copy Markdown
Contributor

@Arths17 Arths17 commented Apr 5, 2026

This PR resolves Issue #3008, which reported a TODO internal error in Expr::attr_infer_for_type. The root cause was that typing.Self, when used invalidly outside of a class context, was being reported by the binding phase but was still "leaking" as an unresolved special form into the inference engine.

When the engine attempted an attribute lookup on this raw Self form, it hit an undefined state. This patch ensures that any Self forms remaining after class-context substitution are collapsed to Any (Error type), allowing the type-checker to proceed gracefully using the already-emitted diagnostic.

Changes

  • pyrefly/lib/alt/solve.rs: Added logic in the annotation solver to detect and normalize dangling Self special forms to Any.
    
  • pyrefly/lib/test/typing_self.rs: Added a regression test test_self_outside_class_no_internal_error_via_classmethod_trampoline which specifically targets the classmethod trampoline pattern identified in the issue.
    

Validation Results

  • **Targeted Test**: test_self_outside_class_no_internal_error_via_classmethod_trampoline → **PASSED**
    
  • **Subsystem Suite**: cargo test typing_self (31 tests) → **PASSED**
    
  • **Regression Suite**: cargo test test::attributes:: (161 tests) → **PASSED**
    
  • **Formatting**: cargo fmt → **Clean**
    
  • **Linting**: cargo clippy was run. Note that current workspace failures in protocol_types.rs and tsp_types are pre-existing technical debt and unrelated to these changes.
    

This pull request addresses a regression related to the handling of the Self special form in type annotations, particularly when it appears outside of a class context. The main fix ensures that invalid uses of Self are replaced with Any to prevent internal errors in later phases. Additionally, a regression test is added to verify the fix.

Type system robustness:

  • Updated AnswersSolver in solve.rs to replace unresolved Self special forms with Any during annotation processing, preventing internal errors when Self is used outside a class.

Testing:

Copilot AI review requested due to automatic review settings April 5, 2026 06:15
@meta-cla meta-cla Bot added the cla signed label Apr 5, 2026
@github-actions github-actions Bot added the size/s label Apr 5, 2026
Copy link
Copy Markdown

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

Fixes a type-checker internal error caused by typing.Self leaking into later inference phases when used invalidly (outside a class context), by normalizing unresolved Self special forms to an error-Any after class-context substitution.

Changes:

  • Update the annotation solving path to replace any remaining SpecialForm::SelfType with Any (error) to prevent downstream undefined states.
  • Add a regression test covering the classmethod “trampoline” pattern from Issue #3008 to ensure the diagnostic is emitted without an internal error.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pyrefly/lib/alt/solve.rs Normalizes dangling Self special forms to error-Any during annotation solving to avoid internal errors in later inference.
pyrefly/lib/test/typing_self.rs Adds a regression test reproducing the Issue #3008 pattern (invalid Self outside class + attribute access) to ensure no internal error occurs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

Looks good to me, I'll try to get a second review / get it merged. Thanks!

@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Apr 6, 2026

@stroxler has imported this pull request. If you are a Meta employee, you can view this in D99678244.

Copy link
Copy Markdown
Contributor

@yangdanny97 yangdanny97 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@stroxler stroxler self-assigned this Apr 6, 2026
@meta-codesync meta-codesync Bot closed this in 06213fd Apr 6, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented Apr 6, 2026

@stroxler merged this pull request in 06213fd.

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.

4 participants