From 91515d67f704019b0a1d28520e7c55bb02b487cf Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Sat, 17 Jun 2023 15:16:13 +0200 Subject: [PATCH 01/10] fix: disallow toplevel infix definitions for vals, vars, givens, methods and implicits Part of #17738 --- .../tools/dotc/reporting/ErrorMessageID.scala | 1 + .../dotty/tools/dotc/reporting/messages.scala | 10 ++++++++++ .../src/dotty/tools/dotc/typer/Checking.scala | 1 + tests/neg/i17738-toplevel-infix.check | 20 +++++++++++++++++++ tests/neg/i17738-toplevel-infix.scala | 14 +++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 tests/neg/i17738-toplevel-infix.check create mode 100644 tests/neg/i17738-toplevel-infix.scala diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index 726cdc7131c4..a4266a84d1f7 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -199,6 +199,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case ClosureCannotHaveInternalParameterDependenciesID // errorNumber: 183 case MatchTypeNoCasesID // errorNumber: 184 case UnimportedAndImportedID // errorNumber: 185 + case ToplevelDefCantBeInfixID // errorNumber: 186 def errorNumber = ordinal - 1 diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 970c44d54cbc..cd10cd8eb0c8 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2938,3 +2938,13 @@ class ClosureCannotHaveInternalParameterDependencies(mt: Type)(using Context) i"""cannot turn method type $mt into closure |because it has internal parameter dependencies""" def explain(using Context) = "" + +class ToplevelDefCantBeInfix(sym: Symbol)(using Context) + extends SyntaxMsg(ToplevelDefCantBeInfixID): + def msg(using Context) = i"a toplevel $defName cannot be infix" + def explain(using Context) = "" + private val defName = + if sym.flags.is(Method) then "def" + else if sym.flags.is(Mutable) then "var" + else if sym.flags.is(Given) then "given" + else "val" diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 7ca99df2bea3..edb7c4bc9f78 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -561,6 +561,7 @@ object Checking { fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden)) sym.setFlag(Private) // break the overriding relationship by making sym Private } + if sym.isWrappedToplevelDef && !sym.isType then fail(ToplevelDefCantBeInfix(sym)) checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given) && !sym.isType || sym.isClass) checkCombination(Final, Open) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check new file mode 100644 index 000000000000..050854e19344 --- /dev/null +++ b/tests/neg/i17738-toplevel-infix.check @@ -0,0 +1,20 @@ +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:10:10 ---------------------------------------------------- +10 |infix val toplevelVal = ??? // error + | ^ + | a toplevel val cannot be infix +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:11:10 ---------------------------------------------------- +11 |infix var toplevelVar = ??? // error + | ^ + | a toplevel var cannot be infix +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:12:10 ---------------------------------------------------- +12 |infix def toplevelDef = ??? // error + | ^ + | a toplevel def cannot be infix +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:12 ---------------------------------------------------- +13 |infix given toplevelGiven: Int = ??? // error + | ^ + | a toplevel given cannot be infix +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:19 ---------------------------------------------------- +14 |infix implicit val topevelImplicit: Int = ??? // error + | ^ + | a toplevel val cannot be infix diff --git a/tests/neg/i17738-toplevel-infix.scala b/tests/neg/i17738-toplevel-infix.scala new file mode 100644 index 000000000000..8bb72967ed07 --- /dev/null +++ b/tests/neg/i17738-toplevel-infix.scala @@ -0,0 +1,14 @@ +infix type A[b, a] = Nothing + +infix type B[b, a] = b match { + case Int => a +} + +infix class C[A, B] +infix trait D[A, B] + +infix val toplevelVal = ??? // error +infix var toplevelVar = ??? // error +infix def toplevelDef = ??? // error +infix given toplevelGiven: Int = ??? // error +infix implicit val topevelImplicit: Int = ??? // error From 1dd1af86dee6fd00804f96b5453d3ead17d38bfb Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Sat, 17 Jun 2023 15:41:50 +0200 Subject: [PATCH 02/10] actually check if a symbol is infix before failing with ToplevelDefCantBeInfix --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index edb7c4bc9f78..5186c491a642 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -561,7 +561,7 @@ object Checking { fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden)) sym.setFlag(Private) // break the overriding relationship by making sym Private } - if sym.isWrappedToplevelDef && !sym.isType then fail(ToplevelDefCantBeInfix(sym)) + if sym.isWrappedToplevelDef && !sym.isType && sym.flags.is(Infix) then fail(ToplevelDefCantBeInfix(sym)) checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given) && !sym.isType || sym.isClass) checkCombination(Final, Open) From 08ddbac08ae3228360dd0edecbe7a0d4d783c4dc Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Sat, 17 Jun 2023 16:05:02 +0200 Subject: [PATCH 03/10] allow infix extension methods --- .../src/dotty/tools/dotc/typer/Checking.scala | 4 +++- tests/neg/i17738-toplevel-infix.check | 20 +++++++++---------- tests/neg/i17738-toplevel-infix.scala | 3 +++ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 5186c491a642..d8c236cff492 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -561,7 +561,9 @@ object Checking { fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden)) sym.setFlag(Private) // break the overriding relationship by making sym Private } - if sym.isWrappedToplevelDef && !sym.isType && sym.flags.is(Infix) then fail(ToplevelDefCantBeInfix(sym)) + + if sym.isWrappedToplevelDef && !sym.isType && sym.flags.is(Infix, butNot = Extension) then + fail(ToplevelDefCantBeInfix(sym)) checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given) && !sym.isType || sym.isClass) checkCombination(Final, Open) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check index 050854e19344..12f57fa3eeed 100644 --- a/tests/neg/i17738-toplevel-infix.check +++ b/tests/neg/i17738-toplevel-infix.check @@ -1,20 +1,20 @@ --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:10:10 ---------------------------------------------------- -10 |infix val toplevelVal = ??? // error +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:10 ---------------------------------------------------- +13 |infix val toplevelVal = ??? // error | ^ | a toplevel val cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:11:10 ---------------------------------------------------- -11 |infix var toplevelVar = ??? // error +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:10 ---------------------------------------------------- +14 |infix var toplevelVar = ??? // error | ^ | a toplevel var cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:12:10 ---------------------------------------------------- -12 |infix def toplevelDef = ??? // error +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- +15 |infix def toplevelDef = ??? // error | ^ | a toplevel def cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:12 ---------------------------------------------------- -13 |infix given toplevelGiven: Int = ??? // error +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:12 ---------------------------------------------------- +16 |infix given toplevelGiven: Int = ??? // error | ^ | a toplevel given cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:19 ---------------------------------------------------- -14 |infix implicit val topevelImplicit: Int = ??? // error +-- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:19 ---------------------------------------------------- +17 |infix implicit val topevelImplicit: Int = ??? // error | ^ | a toplevel val cannot be infix diff --git a/tests/neg/i17738-toplevel-infix.scala b/tests/neg/i17738-toplevel-infix.scala index 8bb72967ed07..f91cc01faea8 100644 --- a/tests/neg/i17738-toplevel-infix.scala +++ b/tests/neg/i17738-toplevel-infix.scala @@ -7,6 +7,9 @@ infix type B[b, a] = b match { infix class C[A, B] infix trait D[A, B] +extension (x: Boolean) + infix def or (y: => Boolean) = x || y + infix val toplevelVal = ??? // error infix var toplevelVar = ??? // error infix def toplevelDef = ??? // error From 2dcf13329b7b1995ae80bfbf67a8944da2682d6b Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Tue, 20 Jun 2023 10:13:33 +0200 Subject: [PATCH 04/10] add newline at the end of 'messages.scala' --- compiler/src/dotty/tools/dotc/reporting/messages.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index cd10cd8eb0c8..390c3325fa9d 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2938,7 +2938,7 @@ class ClosureCannotHaveInternalParameterDependencies(mt: Type)(using Context) i"""cannot turn method type $mt into closure |because it has internal parameter dependencies""" def explain(using Context) = "" - + class ToplevelDefCantBeInfix(sym: Symbol)(using Context) extends SyntaxMsg(ToplevelDefCantBeInfixID): def msg(using Context) = i"a toplevel $defName cannot be infix" @@ -2948,3 +2948,4 @@ class ToplevelDefCantBeInfix(sym: Symbol)(using Context) else if sym.flags.is(Mutable) then "var" else if sym.flags.is(Given) then "given" else "val" + From 03a2ae0e389ccd4aeb443b0dd6376c27488c382c Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Tue, 20 Jun 2023 10:19:14 +0200 Subject: [PATCH 05/10] another newline adjustment --- compiler/src/dotty/tools/dotc/reporting/messages.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index 390c3325fa9d..d9ba6fb37ee8 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2948,4 +2948,3 @@ class ToplevelDefCantBeInfix(sym: Symbol)(using Context) else if sym.flags.is(Mutable) then "var" else if sym.flags.is(Given) then "given" else "val" - From 3f3bd01d4aba5f4dfc0149693d11d82a787f83ee Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Sat, 1 Jul 2023 14:26:33 +0200 Subject: [PATCH 06/10] change error code from 183 to 186 in checkfile --- tests/neg/i17738-toplevel-infix.check | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check index 12f57fa3eeed..985eebd62ca7 100644 --- a/tests/neg/i17738-toplevel-infix.check +++ b/tests/neg/i17738-toplevel-infix.check @@ -1,20 +1,20 @@ --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:10 ---------------------------------------------------- +-- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:10 ---------------------------------------------------- 13 |infix val toplevelVal = ??? // error | ^ | a toplevel val cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:10 ---------------------------------------------------- +-- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:10 ---------------------------------------------------- 14 |infix var toplevelVar = ??? // error | ^ | a toplevel var cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- +-- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- 15 |infix def toplevelDef = ??? // error | ^ | a toplevel def cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:12 ---------------------------------------------------- +-- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:12 ---------------------------------------------------- 16 |infix given toplevelGiven: Int = ??? // error | ^ | a toplevel given cannot be infix --- [E183] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:19 ---------------------------------------------------- +-- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:19 ---------------------------------------------------- 17 |infix implicit val topevelImplicit: Int = ??? // error | ^ | a toplevel val cannot be infix From 2b391c82de1861d8ab0196e78dd2975539b8082a Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Sat, 8 Jul 2023 15:54:46 +0200 Subject: [PATCH 07/10] delete ToplevelDefCantBeInfix (along with its MessageId) and use ModifierNotAllowed with a custom explanation in its place --- .../tools/dotc/reporting/ErrorMessageID.scala | 1 - .../dotty/tools/dotc/reporting/messages.scala | 14 +---- .../src/dotty/tools/dotc/typer/Checking.scala | 8 ++- tests/neg/i17738-toplevel-infix.check | 55 ++++++++++++++----- tests/neg/i17738-toplevel-infix.scala | 1 + 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala index a4266a84d1f7..726cdc7131c4 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala @@ -199,7 +199,6 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe case ClosureCannotHaveInternalParameterDependenciesID // errorNumber: 183 case MatchTypeNoCasesID // errorNumber: 184 case UnimportedAndImportedID // errorNumber: 185 - case ToplevelDefCantBeInfixID // errorNumber: 186 def errorNumber = ordinal - 1 diff --git a/compiler/src/dotty/tools/dotc/reporting/messages.scala b/compiler/src/dotty/tools/dotc/reporting/messages.scala index d9ba6fb37ee8..e2009f2ca668 100644 --- a/compiler/src/dotty/tools/dotc/reporting/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/messages.scala @@ -2551,10 +2551,10 @@ class AnonymousInstanceCannotBeEmpty(impl: untpd.Template)(using Context) |""" } -class ModifierNotAllowedForDefinition(flag: Flag)(using Context) +class ModifierNotAllowedForDefinition(flag: Flag, explanation: String = "")(using Context) extends SyntaxMsg(ModifierNotAllowedForDefinitionID) { def msg(using Context) = i"Modifier ${hl(flag.flagsString)} is not allowed for this definition" - def explain(using Context) = "" + def explain(using Context) = explanation } class RedundantModifier(flag: Flag)(using Context) @@ -2938,13 +2938,3 @@ class ClosureCannotHaveInternalParameterDependencies(mt: Type)(using Context) i"""cannot turn method type $mt into closure |because it has internal parameter dependencies""" def explain(using Context) = "" - -class ToplevelDefCantBeInfix(sym: Symbol)(using Context) - extends SyntaxMsg(ToplevelDefCantBeInfixID): - def msg(using Context) = i"a toplevel $defName cannot be infix" - def explain(using Context) = "" - private val defName = - if sym.flags.is(Method) then "def" - else if sym.flags.is(Mutable) then "var" - else if sym.flags.is(Given) then "given" - else "val" diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index d8c236cff492..7a6d7583dbae 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -561,9 +561,13 @@ object Checking { fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden)) sym.setFlag(Private) // break the overriding relationship by making sym Private } - if sym.isWrappedToplevelDef && !sym.isType && sym.flags.is(Infix, butNot = Extension) then - fail(ToplevelDefCantBeInfix(sym)) + val defKind = + if sym.flags.is(Method) then "def" + else if sym.flags.is(Mutable) then "var" + else if sym.flags.is(Given) then "given" + else "val" + fail(ModifierNotAllowedForDefinition(Flags.Infix, s"a toplevel $defKind cannot be infix")) checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given) && !sym.isType || sym.isClass) checkCombination(Final, Open) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check index 985eebd62ca7..d8eceee66e28 100644 --- a/tests/neg/i17738-toplevel-infix.check +++ b/tests/neg/i17738-toplevel-infix.check @@ -1,20 +1,45 @@ --- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:13:10 ---------------------------------------------------- -13 |infix val toplevelVal = ??? // error +-- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:10 ---------------------------------------------------- +14 |infix val toplevelVal = ??? // error | ^ - | a toplevel val cannot be infix --- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:14:10 ---------------------------------------------------- -14 |infix var toplevelVar = ??? // error + | Modifier infix is not allowed for this definition + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | a toplevel val cannot be infix + -------------------------------------------------------------------------------------------------------------------- +-- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- +15 |infix var toplevelVar = ??? // error | ^ - | a toplevel var cannot be infix --- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- -15 |infix def toplevelDef = ??? // error + | Modifier infix is not allowed for this definition + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | a toplevel var cannot be infix + -------------------------------------------------------------------------------------------------------------------- +-- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:10 ---------------------------------------------------- +16 |infix def toplevelDef = ??? // error | ^ - | a toplevel def cannot be infix --- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:12 ---------------------------------------------------- -16 |infix given toplevelGiven: Int = ??? // error + | Modifier infix is not allowed for this definition + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | a toplevel def cannot be infix + -------------------------------------------------------------------------------------------------------------------- +-- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:12 ---------------------------------------------------- +17 |infix given toplevelGiven: Int = ??? // error | ^ - | a toplevel given cannot be infix --- [E186] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:19 ---------------------------------------------------- -17 |infix implicit val topevelImplicit: Int = ??? // error + | Modifier infix is not allowed for this definition + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | a toplevel given cannot be infix + -------------------------------------------------------------------------------------------------------------------- +-- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:18:19 ---------------------------------------------------- +18 |infix implicit val topevelImplicit: Int = ??? // error | ^ - | a toplevel val cannot be infix + | Modifier infix is not allowed for this definition + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | a toplevel val cannot be infix + -------------------------------------------------------------------------------------------------------------------- diff --git a/tests/neg/i17738-toplevel-infix.scala b/tests/neg/i17738-toplevel-infix.scala index f91cc01faea8..f69df0994af3 100644 --- a/tests/neg/i17738-toplevel-infix.scala +++ b/tests/neg/i17738-toplevel-infix.scala @@ -1,3 +1,4 @@ +// scalac: -explain infix type A[b, a] = Nothing infix type B[b, a] = b match { From 46a1403cee16fd077176b4704ce0a84811947a50 Mon Sep 17 00:00:00 2001 From: Aleksander Rainko Date: Wed, 12 Jul 2023 22:27:07 +0200 Subject: [PATCH 08/10] use Symbol#showKind to print a nicer error message, make the explanation into a full sentence. --- compiler/src/dotty/tools/dotc/typer/Checking.scala | 7 +------ tests/neg/i17738-toplevel-infix.check | 10 +++++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 7a6d7583dbae..020a09f3c17c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -562,12 +562,7 @@ object Checking { sym.setFlag(Private) // break the overriding relationship by making sym Private } if sym.isWrappedToplevelDef && !sym.isType && sym.flags.is(Infix, butNot = Extension) then - val defKind = - if sym.flags.is(Method) then "def" - else if sym.flags.is(Mutable) then "var" - else if sym.flags.is(Given) then "given" - else "val" - fail(ModifierNotAllowedForDefinition(Flags.Infix, s"a toplevel $defKind cannot be infix")) + fail(ModifierNotAllowedForDefinition(Flags.Infix, s"A top-level ${sym.showKind} cannot be infix.")) checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given) && !sym.isType || sym.isClass) checkCombination(Final, Open) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check index d8eceee66e28..fede2b2dece2 100644 --- a/tests/neg/i17738-toplevel-infix.check +++ b/tests/neg/i17738-toplevel-infix.check @@ -5,7 +5,7 @@ |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | a toplevel val cannot be infix + | A top-level value cannot be infix. -------------------------------------------------------------------------------------------------------------------- -- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:15:10 ---------------------------------------------------- 15 |infix var toplevelVar = ??? // error @@ -14,7 +14,7 @@ |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | a toplevel var cannot be infix + | A top-level variable cannot be infix. -------------------------------------------------------------------------------------------------------------------- -- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:16:10 ---------------------------------------------------- 16 |infix def toplevelDef = ??? // error @@ -23,7 +23,7 @@ |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | a toplevel def cannot be infix + | A top-level method cannot be infix. -------------------------------------------------------------------------------------------------------------------- -- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:17:12 ---------------------------------------------------- 17 |infix given toplevelGiven: Int = ??? // error @@ -32,7 +32,7 @@ |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | a toplevel given cannot be infix + | A top-level given instance cannot be infix. -------------------------------------------------------------------------------------------------------------------- -- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:18:19 ---------------------------------------------------- 18 |infix implicit val topevelImplicit: Int = ??? // error @@ -41,5 +41,5 @@ |-------------------------------------------------------------------------------------------------------------------- | Explanation (enabled by `-explain`) |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | a toplevel val cannot be infix + | A top-level value cannot be infix. -------------------------------------------------------------------------------------------------------------------- From 000cdd445e4f37b8da3363a762a8c938fee60039 Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Thu, 13 Jul 2023 13:54:41 +0200 Subject: [PATCH 09/10] Update tests/neg/i17738-toplevel-infix.scala --- tests/neg/i17738-toplevel-infix.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/neg/i17738-toplevel-infix.scala b/tests/neg/i17738-toplevel-infix.scala index f69df0994af3..a67ef9618446 100644 --- a/tests/neg/i17738-toplevel-infix.scala +++ b/tests/neg/i17738-toplevel-infix.scala @@ -15,4 +15,4 @@ infix val toplevelVal = ??? // error infix var toplevelVar = ??? // error infix def toplevelDef = ??? // error infix given toplevelGiven: Int = ??? // error -infix implicit val topevelImplicit: Int = ??? // error +infix implicit val toplevelImplicit: Int = ??? // error From ef786fff0e49cc66d4743e94d90a56afb8ac364f Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Thu, 13 Jul 2023 14:46:18 +0200 Subject: [PATCH 10/10] Update tests/neg/i17738-toplevel-infix.check --- tests/neg/i17738-toplevel-infix.check | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/neg/i17738-toplevel-infix.check b/tests/neg/i17738-toplevel-infix.check index fede2b2dece2..a4946e7afb97 100644 --- a/tests/neg/i17738-toplevel-infix.check +++ b/tests/neg/i17738-toplevel-infix.check @@ -35,7 +35,7 @@ | A top-level given instance cannot be infix. -------------------------------------------------------------------------------------------------------------------- -- [E156] Syntax Error: tests/neg/i17738-toplevel-infix.scala:18:19 ---------------------------------------------------- -18 |infix implicit val topevelImplicit: Int = ??? // error +18 |infix implicit val toplevelImplicit: Int = ??? // error | ^ | Modifier infix is not allowed for this definition |--------------------------------------------------------------------------------------------------------------------