Skip to content

Annotation argument is not a constant #3912

@akshaal

Description

@akshaal

Scalafmt shouldn't reformat code the way that scala can't compile.

Configuration (required)

version = 3.8.1
preset = IntelliJ
runner.dialect = scala3
maxColumn = 120
indent.main = 4
indent.callSite = 8
indent.ctrlSite = 8
indent.defnSite = 8
indent.ctorSite = 16
indent.extendSite = 16
indent.caseSite = 8
indentOperator.excludeRegex = "^$"
indentOperator.includeRegex = ""
align.preset = none
assumeStandardLibraryStripMargin = true
align.stripMargin = true
optIn.annotationNewlines = false
newlines.afterCurlyLambdaParams = squash
newlines.topLevelStatementBlankLines = [ { maxNest = 0, blanks = 2 } ]
newlines.beforeMultiline = unfold
newlines.forceBeforeMultilineAssign = any
newlines.alwaysBeforeElseAfterCurlyIf = false
newlines.beforeCurlyLambdaParams = multilineWithCaseOnly
newlines.avoidForSimpleOverflow = [tooLong, punct]
newlines.inInterpolation = avoid
optIn.configStyleArguments = true
runner.optimizer.forceConfigStyleOnOffset = 60
runner.optimizer.forceConfigStyleMinArgCount = 2
rewrite.rules = [AvoidInfix, RedundantBraces, RedundantParens, SortModifiers, PreferCurlyFors, Imports]
rewrite.redundantBraces.stringInterpolation = true
rewrite.redundantBraces.generalExpressions = true
rewrite.redundantBraces.ifElseExpressions = true
rewrite.redundantBraces.defnBodies = true
rewrite.redundantBraces.maxBreaks = 0
rewrite.insertBraces.minLines = 1
rewrite.imports.sort = original
rewrite.trailingCommas.style = always
docstrings.style = Asterisk
docstrings.removeEmpty = true
docstrings.oneline = unfold
spaces.inImportCurlyBraces = true
includeNoParensInSelectChains = true
includeCurlyBraceInSelectChains = true
runner.dialectOverride.allowSignificantIndentation = false
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.newSyntax.control = false
rewrite.scala3.newSyntax.deprecated = true
rewrite.scala3.removeOptionalBraces.enabled = false

Command-line parameters (required)

Steps

Given code like this:

@ComponentScans(value =
    Array(
            new ComponentScan(
                    value = Array("xxx.werr.werwer.fghfgfh"),
                    nameGenerator = classOf[BeanNameGenerator],
                    lazyInit = true,
            ),
            new ComponentScan(
                    value = Array("xxx.werr.werwer.dsqrtybx"),
                    nameGenerator = classOf[BeanNameGenerator],
                    lazyInit = true,
            ),
    )
)
class WutAppSpringCfg extends SomeOtherStuff {}

compiles just fine

Problem

Scalafmt formats code like this (scalafmt adds brackets around the outer Array as requested by the config):

@ComponentScans(value = {
    Array(
            new ComponentScan(
                    value = Array("xxx.werr.werwer.fghfgfh"),
                    nameGenerator = classOf[BeanNameGenerator],
                    lazyInit = true,
            ),
            new ComponentScan(
                    value = Array("xxx.werr.werwer.dsqrtybx"),
                    nameGenerator = classOf[BeanNameGenerator],
                    lazyInit = true,
            ),
    )
})
class WutAppSpringCfg extends SomeOtherStuff {}

which fails to compile with

..........: Annotation argument is not a constant

Expectation

I would like the formatted output to be compatible with the scala 3. Even if scala should have probably been able to compile it (scala 2 can actually do that), but as long as scala can't compile such code, scalafmt shouldn't produce such code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions