Skip to content

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Mar 25, 2025

Fixes #22671

Find a name for the error message.

Comment on lines 1539 to 1540
|Also, assignment syntax can be used if there is a corresponding setter:
| ${hl("def")} ${name}${hl("_=(x: Int): Unit = _v = x")}
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't add this one here. The sugaring for setters is very very scope specific and fragile. It cannot be summarized into the assignment syntax can be used if there is a corresponding setter...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think some words are necessary for user who needs an explanation; I restricted it to class members.

class ReassignmentToVal(name: Name)(using Context)
extends TypeMsg(ReassignmentToValID) {
def msg(using Context) = i"""Reassignment to val $name"""
def msg(using Context) = i"""Reassignment to value $name"""
Copy link
Member

Choose a reason for hiding this comment

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

We could actually pass the symbol instead of the name and we would either get val, def,... depending on the actual symbol.

@som-snytt som-snytt force-pushed the issue/22671-assignment-error branch from ef97824 to 76c6790 Compare March 28, 2025 16:09
@som-snytt
Copy link
Contributor Author

I started down the long tail of polishing words.

Let the symbol say what it is, and restrict setter explanation to class-owned symbols. (Need to check class params.)

@som-snytt som-snytt force-pushed the issue/22671-assignment-error branch 2 times, most recently from 7785a12 to 21d48c8 Compare March 29, 2025 21:37
@som-snytt som-snytt force-pushed the issue/22671-assignment-error branch from 21d48c8 to 1187728 Compare July 24, 2025 19:55
@som-snytt
Copy link
Contributor Author

Updated the branch from March, which had the spree contributors.

Improved to include the pathology from neg/assignments: x_= = 42. Some words for the user, and also don't try to format a message such as var x_=_= or similar, which throws in the formatter.

That test also suggested providing the args, which I took to mean show x_=(42).

I ran out of steam and did not add the scala 2 explanation, that x += 1 "does not convert to assignment" because the x is not mutable. The existing message that "it doesn't have a += member" is confusing, not only to beginners.

@som-snytt som-snytt force-pushed the issue/22671-assignment-error branch from 1187728 to fb79c19 Compare July 24, 2025 20:16
@som-snytt som-snytt marked this pull request as ready for review July 24, 2025 20:17
@Gedochao Gedochao requested a review from hamzaremmal July 25, 2025 05:31
-- [E052] Type Error: tests/neg/i22671.scala:20:4 ----------------------------------------------------------------------
20 | y = 27 // error
| ^^^^^^
| Assignment to method x
Copy link
Member

Choose a reason for hiding this comment

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

Seems like the renaming isn't taken into account here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that was intentional, though it could be improved

Assignment to y which is a renaming of method x

or

Assignment to method x imported as y // closer to user syntax

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I think this PR would profit from a discussion on concrete test cases what the error message should be. Concentrate in common test cases not obscure ones. Obscure code does not warrant much work to make clear error messages.

So, can we break out the test files into a comment, show what the current message is, and what the suggested error message should be?

-- [E052] Type Error: tests/neg/assignments.scala:17:8 -----------------------------------------------------------------
17 | x_= = 2 // error should give missing arguments, was: Reassignment to val x_=
| ^^^^^^^
| Bad assignment to setter should use x_=(2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I don't see how this is an improvement. Who in their right minds would write x_= =? That's just not what I see, and we should not special case it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was an existing test. And little cost to cover the case.

3 | val updateText2 = copy(text = (_: String)) // error
| ^^^^^^^^^^^^^^^^^^
| Reassignment to val text
| Assignment to value text
Copy link
Contributor

@odersky odersky Oct 15, 2025

Choose a reason for hiding this comment

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

I find "Reassignment to val" much clearer than "Assignment to value".

Copy link
Member

Choose a reason for hiding this comment

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

I agree

Co-authored-by: Jan-Pieter van den Heuvel <[email protected]>
Co-authored-by: Seth Tisue <[email protected]>
Co-authored-by: Lucas Nouguier <[email protected]>
Co-authored-by: Prince <[email protected]>
@som-snytt som-snytt force-pushed the issue/22671-assignment-error branch from a9a08b4 to b40dc93 Compare October 15, 2025 14:39
@som-snytt
Copy link
Contributor Author

som-snytt commented Oct 15, 2025

The PR includes only the fix for the issue and not the semester project in psychology of programming.

I think "reassignment to val" is merely familiar and corresponds to a certain mental model (that initialization or definition is one assignment, and a subsequent use of = is a "reassignment"). If only we had :=.

The goal was parity with Scala 2, where errors in assignment operators += can be obscure, especially because it may be "not a member" or it may be that the rewrite failed for some other reason.

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.

Error says none for overloaded symbol

3 participants