Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Erase constant value type [ci: last-only] #9431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 2.13.x
Choose a base branch
from

Conversation

som-snytt
Copy link
Contributor

Mixin was confused when String("hello, world") overrides String.

Fixes scala/bug#12301

@scala-jenkins scala-jenkins added this to the 2.13.5 milestone Jan 12, 2021
@som-snytt
Copy link
Contributor Author

som-snytt commented Jan 12, 2021

Quick lunchtime fix. This probably breaks overriding a method of literal type. (Edit: that works after all. I was wondering about the asymmetry of the fix.)

@som-snytt som-snytt marked this pull request as draft January 14, 2021 20:29
@som-snytt som-snytt closed this Jan 19, 2021
@SethTisue SethTisue removed this from the 2.13.5 milestone Jan 20, 2021
@som-snytt som-snytt reopened this Mar 31, 2022
@scala-jenkins scala-jenkins added this to the 2.13.9 milestone Mar 31, 2022
@SethTisue SethTisue modified the milestones: 2.13.9, 2.13.10 Apr 25, 2022
@lrytz lrytz modified the milestones: 2.13.10, 2.13.11 Sep 26, 2022
@SethTisue
Copy link
Member

@som-snytt is this one you're thinking of reviving...?

@som-snytt
Copy link
Contributor Author

@SethTisue yes, although like all old relationships, I've forgotten what was so attractive about it.

@SethTisue SethTisue modified the milestones: 2.13.11, 2.13.12 Feb 28, 2023
@som-snytt som-snytt marked this pull request as ready for review March 7, 2023 06:17
@som-snytt som-snytt modified the milestones: 2.13.12, 2.13.11 Mar 7, 2023
@som-snytt som-snytt changed the title Constant type matches widened type Erase constant value type Mar 7, 2023
@som-snytt som-snytt marked this pull request as draft March 7, 2023 08:53
@SethTisue SethTisue modified the milestones: 2.13.11, 2.13.12 Mar 13, 2023
@som-snytt som-snytt closed this Jun 5, 2023
@SethTisue SethTisue removed this from the 2.13.12 milestone Jun 10, 2023
@som-snytt som-snytt reopened this Jan 27, 2025
@scala-jenkins scala-jenkins added this to the 2.13.17 milestone Jan 27, 2025
@som-snytt
Copy link
Contributor Author

som-snytt commented Apr 27, 2025

The bug fix is to handle FoldableConstantType correctly in reflect.internal.transform.Erasure.

The rest of the commit is style adjustments while reading code.

It is difficult to see the history above, but the first swing was about matching members; lrytz hinted it's just an erasure problem; and at some point I made that tweak.

A duplicate ticket from a decade ago shows that the symptom has been noticed for some time; that ticket said specialized, so may have been neglected on that basis; it is unknown whether mysterious runtime errors were due to this behavior; OTOH, how often does one override with a constant result.

Edit: maybe this was what gave me pause:

[error] ## Exception when compiling 26 sources to /home/runner/work/scala/scala/build/quick/classes/sbt-bridge
[error] java.lang.ClassFormatError: Method HASH_SIZE in class scala/reflect/internal/Names has illegal modifiers: 0x402

Edit: that was constructors detecting whether to drop RHS, resulting in abstract member.

@som-snytt som-snytt changed the title Erase constant value type Erase constant value type [ci: last-only] Apr 28, 2025
@som-snytt
Copy link
Contributor Author

som-snytt commented Apr 28, 2025

Ran the sbt test locally, thanks to instructions in build.sbt.

[info] [info] compiling 1 Scala source to /tmp/sbt_6b9da7a3/target/classes ...
[info] [error] /tmp/sbt_6b9da7a3/A.scala:3:20: type mismatch;
[info] [error]  found   : Int
[info] [error]  required: String
[info] [error]  val z: String = B.y
[info] [error]                    ^
[info] [error] one error found
[info] [error] (Compile / compileIncremental) Compilation failed
[info] [error] Total time: 0 s, completed Apr 28, 2025, 1:53:32 PM
[error] x source-dependencies/false-error [PENDING]
[error]  Cause of test exception: {line 7}  Command failed: compile failed

It's supposed to recompile and notice B.y is now a String. From the test name, it looks like erroring was the old behavior.

Or is it supposed to be skipped as "pending" not "test"? What's going on? (That is, I have to learn something new again?)

All the PENDING are failures.

@lrytz
Copy link
Member

lrytz commented Apr 29, 2025

The repording for those pending tests is very confusing :-/

[error] x source-dependencies/false-error [PENDING]
[error]  Cause of test exception: {line 7}  Command failed: compile failed

is its way of saying "I ran source-dependencies/false-error and it failed, as expected, since it's marked pending".

So the scripted tests look clean.

The real error seems to be

[error] Test scala.reflect.ClassOfTest.classOfUnitConstant failed: java.lang.ClassFormatError: Duplicate method name "f" with signature "()Ljava.lang.Class;" in class file __wrapper$4$8c32535ff2d0445a8346dfeff9e261a4/__wrapper$4$8c32535ff2d0445a8346dfeff9e261a4$C$1, took 0.094 sec

@som-snytt
Copy link
Contributor Author

D'oh, I didn't scroll back far enough. It's so hard to keep up with these important computering skills.

@som-snytt
Copy link
Contributor Author

The other important skill is when the comment says "special case", to preserve the special case.

My other style preference that has evolved is to avoid long lines where the important bit is pushed too far to the right. Less important side effects can be pushed right, speaking typographically not politically. Then vertical alignment becomes a matter of aligning important bits, as we learned when writing out addition with a pencil.

NoSymbol
)
final def overridingSymbol(ofclazz: Symbol): Symbol =
if (canMatchInheritedSymbols) matchingSymbol(ofclazz, ofclazz.thisType) else NoSymbol
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One-liner has the same form as previous method; this works well because the else alternative is trivial.

@@ -609,7 +607,7 @@ abstract class Constructors extends Statics with Transform with TypingTransforme

private def triage() = {
// Constant typed vals are not memoized.
def memoizeValue(sym: Symbol) = !sym.info.resultType.isInstanceOf[FoldableConstantType]
def memoizeValue(sym: Symbol) = enteringErasure(!sym.info.resultType.isInstanceOf[FoldableConstantType])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why it's important to study history.

deriveDefDef(stat)(_ => EmptyTree)
}
else dd
defBuf += toMove
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 used to think vertical boolean exprs (wrapped in parens) were too much style just to have leading infix operators, but after using actual leading infix (without parens), I find it much easier to read. I only preferred the one-liner because mentally I would skip it and that's why it felt easier to read.

note that under -Xsource:3 we could be using leading infix.

if (ct.tag == ClazzTag)
if (ct.typeValue.typeSymbol == UnitClass) tp
else ConstantType(Constant(apply(ct.typeValue)))
else ct.tpe
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fix for erasure. There are really 2 special cases under Class constant. Presumably the Unit case is handled specially later, but from this perspective, the encoding doesn't look regular.

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.

final val overriding def: Duplicate method name
4 participants