Skip to content

Give literal expressions their type - #897

Closed
Firehed wants to merge 5 commits into
mainfrom
838/scalar-literal-types
Closed

Firehed wants to merge 5 commits into
mainfrom
838/scalar-literal-types

Conversation

@Firehed

@Firehed Firehed commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Closes #838.

Problem

A variable assigned a literal has no type:

$count = 2;
$count; // hover shows mixed

Cause

A variable takes the type of the expression assigned to it, through ExpressionResolver::resolve(). That method had no case for literals, so they resolved to nothing.

Change

ExpressionResolver::resolve() gives a literal the narrowest PHP type that names its value:

  • integer literals: int. One too large for int is a float in PHP, and the parser reads it as one: float
  • float literals: float
  • string literals, with or without interpolation, including heredoc and nowdoc: string
  • __LINE__: int; every other magic constant: string
  • true, false, null: the standalone types true, false, null

PHP reads true, false, and null as literals in any letter case, but only after aliases and namespaces apply to the name. use const Foo\BAR as true; makes true (not TRUE, since constant aliases match letter case exactly) name Foo\BAR, and B\true is a namespaced constant. The parser's name resolution follows the same rules, so the check compares the resolved name, whole, ignoring letter case.

Visible behavior

  • Hover on, and variable completion details for, a variable assigned a literal show its type.
  • Hover on true, false, or null shows the type. Before, lowercase showed nothing and uppercase showed const TRUE.

Known limits

  • A ternary or ?? still resolves to the first branch that resolves. Because literals now resolve, a literal written first wins: $c ? null : new User() resolves to null where it used to resolve to User. PHP gives ?User. Likewise $arr['k'] ?? 0 used to have no type, because the array read does not resolve, and now resolves to int. Conditional typing is tracked as a whole in Conditional expressions must take the type of every branch they can produce #896, and tests/EndToEnd/defects/896-null-first-ternary.php reproduces the null-first case. Without null resolving, that case passes.
  • Arrays, unary minus (-1), and arithmetic are not literals and stay untyped.
  • Constants still carry no type from their values.

testArrowFunctionResolvesThroughToItsEnclosingFunction expected no type for $outer = 1;; it now expects int.

🤖 Generated with Claude Code

Firehed and others added 2 commits November 2, 2026 14:29
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.71%. Comparing base (44f6a1f) to head (34a6b9e).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #897   +/-   ##
=========================================
  Coverage     99.71%   99.71%           
- Complexity     1970     1979    +9     
=========================================
  Files           147      147           
  Lines          4488     4502   +14     
=========================================
+ Hits           4475     4489   +14     
  Misses           13       13           

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

Firehed and others added 3 commits November 2, 2026 14:42
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Firehed

Firehed commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

This does not do what it's supposed to do. Bad claude.

@Firehed Firehed closed this Nov 2, 2026
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.

Variables assigned a scalar literal must take the literal's type

1 participant