From edcc705ae4d102736fb49a8b07db50fd37acb74b Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 8 Aug 2023 15:14:50 +0200 Subject: [PATCH 1/2] Fix adaptation of constants to constant type aliases Fixes #18340 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- tests/neg/i18340.scala | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i18340.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 3aaf4fec59d6..fc1b90ea356d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -4162,7 +4162,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer || x.tag == LongTag && cls == defn.DoubleClass && x.longValue.toDouble.toLong != x.longValue then report.warning(LossyWideningConstantConversion(x.tpe, pt), tree.srcPos) - return adaptConstant(tree, ConstantType(converted)) + return readapt(adaptConstant(tree, ConstantType(converted))) case _ => val captured = captureWildcardsCompat(wtp, pt) diff --git a/tests/neg/i18340.scala b/tests/neg/i18340.scala new file mode 100644 index 000000000000..ebbc4526d527 --- /dev/null +++ b/tests/neg/i18340.scala @@ -0,0 +1,9 @@ +@main def main: Unit = + type T = 3f + val value0: T = -3.5f // error + val value1: T = -100500 // error + val value2: T = -100500L // error + val value3: T = -100500D // error + val value4: T = true // error + val value5: 3f = -100500 // error + val value6: 3f = -100500L // error \ No newline at end of file From fab90efcb02468f2617fa095fb9b2a91cf975191 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 9 Aug 2023 11:03:06 +0100 Subject: [PATCH 2/2] Add the other test combination --- tests/neg/i18340.scala | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/neg/i18340.scala b/tests/neg/i18340.scala index ebbc4526d527..a16d506ee196 100644 --- a/tests/neg/i18340.scala +++ b/tests/neg/i18340.scala @@ -6,4 +6,31 @@ val value3: T = -100500D // error val value4: T = true // error val value5: 3f = -100500 // error - val value6: 3f = -100500L // error \ No newline at end of file + val value6: 3f = -100500L // error + + type Ti = 3 + val value1i: Ti = -100500 // error + val value2i: Ti = -100500L // error + val value0i: Ti = -100500F // error + val value3i: Ti = -100500D // error + val value4i: Ti = true // error + val value5i: 3 = -100500 // error + val value6i: 3 = -100500L // error + + type Tl = 3L + val value1l: Tl = -100500 // error + val value2l: Tl = -100500L // error + val value0l: Tl = -100500F // error + val value3l: Tl = -100500D // error + val value4l: Tl = true // error + val value5l: 3L = -100500 // error + val value6l: 3L = -100500L // error + + type Td = 3D + val value1d: Td = -100500 // error + val value2d: Td = -100500L // error + val value0d: Td = -100500F // error + val value3d: Td = -100500D // error + val value4d: Td = true // error + val value5d: 3D = -100500 // error + val value6d: 3D = -100500L // error