8000 Introduce `Transparent` flag · scala/scala3@d10d39a · GitHub
[go: up one dir, main page]

Skip to content

Commit d10d39a

Browse files
committed
Introduce Transparent flag
Let the `transparent` modifier translate to it. allow `transparent` on traits and inline methods only.
1 parent 1b7d63c commit d10d39a

File tree

10 files changed

+29
-20
lines changed

10 files changed

+29
-20
lines changed

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
212212

213213
case class Inline()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Inline)
214214

215-
case class Transparent()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.EmptyFlags)
215+
case class Transparent()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Transparent)
216216
}
217217

218218
/** Modifiers and annotations for definitions

compiler/src/dotty/tools/dotc/core/Flags.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ object Flags {
342342
/** Symbol is a Java default method */
343343
val (_, DefaultMethod @ _, _) = newFlags(38, "<defaultmethod>")
344344

345+
/** Symbol is a transparent inline method or trait */
346+
val (Transparent @ _, _, _) = newFlags(39, "transparent")
347+
345348
/** Symbol is an enum class or enum case (if used with case) */
346349
val (Enum @ _, EnumVal @ _, _) = newFlags(40, "enum")
347350

@@ -419,7 +422,7 @@ object Flags {
419422

420423
/** Flags representing source modifiers */
421424
private val CommonSourceModifierFlags: FlagSet =
422-
commonFlags(Private, Protected, Final, Case, Implicit, Given, Override, JavaStatic)
425+
commonFlags(Private, Protected, Final, Case, Implicit, Given, Override, JavaStatic, Transparent)
423426

424427
val TypeSourceModifierFlags: FlagSet =
425428
CommonSourceModifierFlags.toTypeFlags | Abstract | Sealed | Opaque | Open
@@ -449,7 +452,7 @@ object Flags {
449452
* is completed)
450453
*/
451454
val AfterLoadFlags: FlagSet = commonFlags(
452-
FromStartFlags, AccessFlags, Final, AccessorOrSealed, LazyOrTrait, SelfName, JavaDefined)
455+
FromStartFlags, AccessFlags, Final, AccessorOrSealed, LazyOrTrait, SelfName, JavaDefined, Transparent)
453456

454457
/** A value that's unstable unless complemented with a Stable flag */
455458
val UnstableValueFlags: FlagSet = Mutable | Method
@@ -499,7 +502,7 @@ object Flags {
499502
/** Flags that can apply to a module val */
500503
val RetainedModuleValFlags: FlagSet = RetainedModuleValAndClassFlags |
501504
Override | Final | Method | Implicit | Given | Lazy |
502-
Accessor | AbsOverride | StableRealizable | Captured | Synchronized | Erased
505+
Accessor | AbsOverride | StableRealizable | Captured | Synchronized | Erased | Transparent
503506

504507
/** Flags that can apply to a module class */
505508
val RetainedModuleClassFlags: FlagSet = RetainedModuleValAndClassFlags | Enum

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ class TreePickler(pickler: TastyPickler) {
712712
if (flags.is(Local)) writeModTag(LOCAL)
713713
if (flags.is(Synthetic)) writeModTag(SYNTHETIC)
714714
if (flags.is(Artifact)) writeModTag(ARTIFACT)
715+
if flags.is(Transparent) then writeModTag(TRANSPARENT)
715716
if (isTerm) {
716717
if (flags.is(Implicit)) writeModTag(IMPLICIT)
717718
if (flags.is(Given)) writeModTag(GIVEN)

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,13 @@ class TreeUnpickler(reader: TastyReader,
667667
case PARAMalias => addFlag(SuperParamAlias)
668668
case EXPORTED => addFlag(Exported)
669669
case OPEN => addFlag(Open)
670+
case TRANSPARENT => addFlag(Transparent)
670671
case PRIVATEqualified =>
671672
readByte()
672673
privateWithin = readWithin
673674
case PROTECTEDqualified =>
674675
addFlag(Protected)
675676
privateWithin = readWithin
676-
case SUPERTRAIT =>
677-
readByte()
678-
annotFns = (_ => Annotation(defn.MixinAnnot)) :: annotFns
679677
case ANNOTATION =>
680678
annotFns = readAnnot :: annotFns
681679
case tag =>

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,11 +2807,7 @@ object Parsers {
28072807
}
28082808
else
28092809
mods
2810-
val result = normalize(loop(start))
2811-
for case mod @ Mod.Transparent() <- result.mods do
2812-
if !result.is(Inline) then
2813-
syntaxError(em"`transparent` can only be used in conjunction with `inline`", mod.span)
2814-
result
2810+
normalize(loop(start))
28152811
}
28162812

28172813
val funTypeArgMods: BitSet = BitSet(ERASED)

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
897897
if (rawFlags.is(Param)) flagMask = flagMask &~ Given
898898
val flags = rawFlags & flagMask
899899
var flagsText = toTextFlags(sym, flags)
900-
if mods.hasMod(classOf[untpd.Mod.Transparent]) then
901-
flagsText = "transparent " ~ flagsText
902900
val annotations =
903901
if (sym.exists) sym.annotations.filterNot(ann => dropAnnotForModText(ann.symbol)).map(_.tree)
904902
else mods.annotations.filterNot(tree => dropAnnotForModText(tree.symbol))

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ object Checking {
449449
if (sym.isType)
450450
fail(TypesAndTraitsCantBeImplicit())
451451
}
452+
if sym.is(Transparent) then
453+
if sym.isType then
454+
if !sym.is(Trait) then fail(em"`transparent` can only be used for traits")
455+
else
456+
if !sym.isInlineMethod then fail(em"`transparent` can only be used for inline methods")
452457
if (!sym.isClass && sym.is(Abstract))
453458
fail(OnlyClassesCanBeAbstract(sym))
454459
// note: this is not covered by the next test since terms can be abstract (which is a dual-mode flag)

compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ object PrepareInlineable {
206206

207207
/** The type ascription `rhs: tpt`, unless `original` is `transparent`. */
208208
def wrapRHS(original: untpd.DefDef, tpt: Tree, rhs: Tree)(using Context): Tree =
209-
if original.mods.hasMod(classOf[untpd.Mod.Transparent]) then rhs
210-
else Typed(rhs, tpt)
209+
if original.mods.is(Transparent) then rhs else Typed(rhs, tpt)
211210

212211
/** Return result of evaluating `op`, but drop `Inline` flag and `Body` annotation
213212
* of `sym` in case that leads to errors.

tasty/src/dotty/tools/tasty/TastyFormat.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ object TastyFormat {
370370
final val OPEN = 40
371371
final val PARAMEND = 41
372372
final val PARAMalias = 42
373-
final val SUPERTRAIT = 43 // TODO: remove
373+
final val TRANSPARENT = 43
374374

375375
// Cat. 2: tag Nat
376376

@@ -485,7 +485,7 @@ object TastyFormat {
485485

486486
/** Useful for debugging */
487487
def isLegalTag(tag: Int): Boolean =
488-
firstSimpleTreeTag <= tag && tag <= SUPERTRAIT ||
488+
firstSimpleTreeTag <= tag && tag <= TRANSPARENT ||
489489
firstNatTreeTag <= tag && tag <= RENAMED ||
490490
firstASTTreeTag <= tag && tag <= BOUNDED ||
491491
firstNatASTTreeTag <= tag && tag <= NAMEDARG ||
@@ -514,7 +514,7 @@ object TastyFormat {
514514
| STATIC
515515
| OBJECT
516516
| TRAIT
517-
| SUPERTRAIT
517+
| TRANSPARENT
518518
| ENUM
519519
| LOCAL
520520
| SYNTHETIC
@@ -575,7 +575,7 @@ object TastyFormat {
575575
case STATIC => "STATIC"
576576
case OBJECT => "OBJECT"
577577
case TRAIT => "TRAIT"
578-
case SUPERTRAIT => "SUPERTRAIT"
578+
case TRANSPARENT => "TRANSPARENT"
579579
case ENUM => "ENUM"
580580
case LOCAL => "LOCAL"
581581
case SYNTHETIC => "SYNTHETIC"

tests/neg/transparent.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
transparent def foo = 1 // error
2+
transparent inline def bar = 2 // ok
3+
transparent inline val x = 2 // error
4+
transparent class c // error
5+
transparent object y // error
6+
transparent trait t // ok
7+
transparent type T = c // error
8+
transparent given c // error
9+

0 commit comments

Comments
 (0)
0