Skip to content

sway-core: Clarify error for self used without a self parameter#7647

Open
Dnreikronos wants to merge 3 commits into
FuelLabs:masterfrom
Dnreikronos:typecheck/self_param_missing_error
Open

sway-core: Clarify error for self used without a self parameter#7647
Dnreikronos wants to merge 3 commits into
FuelLabs:masterfrom
Dnreikronos:typecheck/self_param_missing_error

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Closes #7645

Using self as a value inside a method that has no self parameter reported "Identifier "self" was used as a variable, but it is actually a generic type parameter." That is misleading: self is a keyword, not a user generic. It happens because both self and Self get inserted into the namespace as the injected Self type parameter, so resolution falls through to it once no real self parameter shadows the name.

Special-case that resolution and emit a new SelfParameterNotAvailable error pointing at the missing self parameter. Self and methods that do take self are unaffected, since a real self parameter resolves to a variable. Adds a should_fail test covering both the method-receiver and bare-value uses.

@Dnreikronos Dnreikronos requested a review from a team as a code owner June 7, 2026 20:07
@fuel-cla-bot

fuel-cla-bot Bot commented Jun 7, 2026

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @blsoaresdev-stack to sign the Fuel Labs Contributor License Agreement.

@cursor

cursor Bot commented Jun 7, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Compiler-only diagnostic change on a narrow name-resolution path; behavior for valid self usage is unchanged.

Overview
Improves compile diagnostics when self is used as a value in an impl method that does not declare a self parameter. Without a real self binding, name resolution used to hit the injected Self generic (GenericTypeForFunctionScope) and report NotAVariable (“actually a generic type parameter”), which was confusing.

Variable expression type-checking now special-cases self resolving to that generic and emits SelfParameterNotAvailable with a message that the enclosing function lacks a self parameter. Self as a type and methods that do take self are unchanged. A should_fail e2e test covers both self.x() and bare self uses.

Reviewed by Cursor Bugbot for commit 44836c2. Bugbot is set up for automated code reviews on this repo. Configure here.

@Dnreikronos Dnreikronos force-pushed the typecheck/self_param_missing_error branch from 4189257 to f1054ee Compare June 7, 2026 20:09
Using `self` as a value in a method without a `self` parameter
resolved to the injected `Self` type parameter, producing the
misleading "used as a variable, but it is actually a generic type
parameter" message.

Detect that case and emit SelfParameterNotAvailable, which points at
the missing `self` parameter instead.

Closes FuelLabs#7645
@Dnreikronos Dnreikronos force-pushed the typecheck/self_param_missing_error branch from f1054ee to 92efa2e Compare June 7, 2026 20:11
@Dnreikronos

Copy link
Copy Markdown
Contributor Author

cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 92efa2e. Configure here.

@codspeed-hq

codspeed-hq Bot commented Jun 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 25 untouched benchmarks


Comparing Dnreikronos:typecheck/self_param_missing_error (44836c2) with master (ac933ff)

Open in CodSpeed

@
Split self-without-self test into separate functions

The filecheck `check`/`nextln` pairs were binding to the wrong error.
With both `self.x()` and bare `self` in one function, the bare `self`
line showed up as trailing context inside the first error block, so the
second `check: let _ = self;` matched there and its `nextln` for the
error message failed (the next line was `}`, not the message).

Putting each use in its own function keeps the two error context windows
from overlapping, so each check/nextln pair binds to its own error.
@
@Dnreikronos Dnreikronos force-pushed the typecheck/self_param_missing_error branch from 86d6013 to 44836c2 Compare June 7, 2026 20:56
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.

Confusing error message on missing self, potentially indicating bigger issues with type parameters

1 participant