Skip to content

AnonymousInstanceCannotBeEmpty error not triggered #24752

@WojciechMazur

Description

@WojciechMazur

Anonymous givens should not be allowed. There is a dedicated error code for this

class AnonymousInstanceCannotBeEmpty(impl: untpd.Template)(using Context)
extends SyntaxMsg(AnonymousInstanceCannotBeEmptyID) {
def msg(using Context) = i"anonymous instance must implement a type or have at least one extension method"
def explain(using Context) =
i"""|Anonymous instances cannot be defined with an empty body. The block
|`${impl.show}` should either contain an implemented type or at least one extension method.
|"""
}

However it seems to be never triggered in

/** Invent a name for an anonymous given of type or template `impl`. */
def inventGivenName(impl: Tree)(using Context): SimpleName =
val str = impl match
case impl: Template =>
if impl.parents.isEmpty then
report.error(AnonymousInstanceCannotBeEmpty(impl), impl.srcPos)
nme.ERROR.toString
else
impl.parents.map(inventTypeName(_)).mkString("given_", "_", "")
case impl: Tree =>
"given_" ++ inventTypeName(impl)
str.toTermName.asSimpleName

Compiler version

3.7.4
3.8.1-nightly

Minimized code

given {}

The question is should anonymous refinements be allowed:

given {def foo: Int} = ???
given {def bar: Int} = ???

Both compile but produce the same name leading to other compilation error

Output

Compiles

Expectation

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:implicitsrelated to implicitsarea:reportingError reporting including formatting, implicit suggestions, etcitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions