Skip to content

Commit 1dae28d

Browse files
authored
use new rather than apply in Scala 3 (#176)
Scala 2 macros expand to e.g. `new sourcecode.Line(2)`. To maintain compatibility with other tools such as Mill's CodeSig analyzer, we should directly call the constructor, rather than the companion's `apply` method e.g. `sourcecode.Line.apply(2)`
1 parent 9b14bfe commit 1dae28d

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

sourcecode/src-3/sourcecode/Macros.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ object Macros {
112112
import quotes.reflect._
113113
val owner = actualOwner(Symbol.spliceOwner)
114114
val simpleName = Util.getName(owner)
115-
'{Name(${Expr(simpleName)})}
115+
'{new Name(${Expr(simpleName)})}
116116
}
117117

118118
private def adjustName(s: String): String =
@@ -126,7 +126,7 @@ object Macros {
126126
import quotes.reflect._
127127
val owner = nonMacroOwner(Symbol.spliceOwner)
128128
val simpleName = adjustName(Util.getName(owner))
129-
'{Name.Machine(${Expr(simpleName)})}
129+
'{new Name.Machine(${Expr(simpleName)})}
130130
}
131131

132132
def fullNameImpl(using Quotes): Expr[FullName] = {
@@ -142,7 +142,7 @@ object Macros {
142142
.filterNot(Util.isSyntheticName)
143143
.map(cleanChunk)
144144
.mkString(".")
145-
'{FullName(${Expr(fullName)})}
145+
'{new FullName(${Expr(fullName)})}
146146
}
147147

148148
def fullNameMachineImpl(using Quotes): Expr[FullName.Machine] = {
@@ -153,34 +153,34 @@ object Macros {
153153
.map(_.stripPrefix("_$").stripSuffix("$")) // meh
154154
.map(adjustName)
155155
.mkString(".")
156-
'{FullName.Machine(${Expr(fullName)})}
156+
'{new FullName.Machine(${Expr(fullName)})}
157157
}
158158

159159
def fileImpl(using Quotes): Expr[sourcecode.File] = {
160160
import quotes.reflect._
161161
val file = quotes.reflect.Position.ofMacroExpansion.sourceFile.path
162-
'{sourcecode.File(${Expr(file)})}
162+
'{new sourcecode.File(${Expr(file)})}
163163
}
164164

165165
def fileNameImpl(using Quotes): Expr[sourcecode.FileName] = {
166166
val name = quotes.reflect.Position.ofMacroExpansion.sourceFile.name
167-
'{sourcecode.FileName(${Expr(name)})}
167+
'{new sourcecode.FileName(${Expr(name)})}
168168
}
169169

170170
def lineImpl(using Quotes): Expr[sourcecode.Line] = {
171171
val line = quotes.reflect.Position.ofMacroExpansion.startLine + 1
172-
'{sourcecode.Line(${Expr(line)})}
172+
'{new sourcecode.Line(${Expr(line)})}
173173
}
174174

175175
def enclosingImpl(using Quotes): Expr[Enclosing] = {
176176
import quotes.reflect._
177177
val path = enclosing(machine = false)(!Util.isSynthetic(_))
178-
'{Enclosing(${Expr(path)})}
178+
'{new Enclosing(${Expr(path)})}
179179
}
180180

181181
def enclosingMachineImpl(using Quotes): Expr[Enclosing.Machine] = {
182182
val path = enclosing(machine = true)(_ => true)
183-
'{Enclosing.Machine(${Expr(path)})}
183+
'{new Enclosing.Machine(${Expr(path)})}
184184
}
185185

186186
def pkgImpl(using Quotes): Expr[Pkg] = {
@@ -189,7 +189,7 @@ object Macros {
189189
case _ => false
190190
}
191191

192-
'{Pkg(${Expr(path)})}
192+
'{new Pkg(${Expr(path)})}
193193
}
194194

195195
def argsImpl(using qctx: Quotes): Expr[Args] = {
@@ -217,21 +217,21 @@ object Macros {
217217

218218
val texts0 = param.map(_.foldRight('{List.empty[Text[_]]}) {
219219
case (vd @ ValDef(nme, _, _), l) =>
220-
'{Text(${Ref(vd.symbol).asExpr}, ${Expr(nme)}) :: $l}
220+
'{(new Text(${Ref(vd.symbol).asExpr}, ${Expr(nme)})) :: $l}
221221
})
222222
val texts = texts0.foldRight('{List.empty[List[Text[_]]]}) {
223223
case (l, acc) =>
224224
'{$l :: $acc}
225225
}
226226

227-
'{Args($texts)}
227+
'{new Args($texts)}
228228
}
229229

230230

231231
def text[T: Type](v: Expr[T])(using Quotes): Expr[sourcecode.Text[T]] = {
232232
import quotes.reflect._
233233
val txt = v.asTerm.pos.sourceCode.get
234-
'{sourcecode.Text[T]($v, ${Expr(txt)})}
234+
'{new sourcecode.Text[T]($v, ${Expr(txt)})}
235235
}
236236

237237
sealed trait Chunk

0 commit comments

Comments
 (0)