Side effects in AnonymousDecl.getImplicitConstructorOpt()

Issue #339 resolved
Jesper Öqvist created an issue

ExtendJ 8.1.2-85-gcff2884 Java SE 8

AnonymousDecl.getImplicitConstructorOpt() calls TypeDecl.nextAnonymousIndex() which incremented a counter, using the result of the counter to modify the AnonymousDecl name with setID().

java4/frontend/AnonymousClasses.jrag:

  public int TypeDecl.anonymousIndex = 0;

  public int TypeDecl.nextAnonymousIndex() {
    if (isNestedType()) {
      return enclosingType().nextAnonymousIndex();
    }
    return anonymousIndex++;
  }

java4/frontend/LookupConstructor.jrag:

  syn lazy Opt<ConstructorDecl> AnonymousDecl.getImplicitConstructorOpt() {
    if (needsImplicitConstructor()) {
      ConstructorDecl decl = constructorDecl();
      Modifiers modifiers = new Modifiers();
      String anonName = "Anonymous" + nextAnonymousIndex();   /// <<<

      ConstructorDecl constructor = new ConstructorDecl(modifiers, anonName,
          constructorParameterList(decl), new List(), new Opt(), new Block());

      setID(anonName);  // <<<<
      ...

This side effect is really only needed for creating unique anonymous class names during semantic analysis. In code generation, unique names are instead generated by the attribute syn lazy String TypeDecl.uniqueName() which is not based on the previously generated anonymous class name.

Comments (2)

  1. Jesper Öqvist reporter

    Remove AnonymousDecl.getImplicitConstructorOpt() side effects

    AnonymousDecl.getImplicitConstructorOpt() called TypeDecl.nextAnonymousIndex() which incremented a counter, using the value of the counter to modify the AnonymousDecl name with setID().

    Anonymous classes are now named #Anonymous. This name must only be distinct from non-anonymous classes. The name is replaced by TypeDecl.uniqueName() during code generation.

    The semantic error rule checking that no class shares the same name as its enclosing class has been updated to allow anonymous classes to have the same name as their enclosing class.

    Fixes #339

    → <<cset 5ed376b93668>>

  2. Log in to comment