Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xtend generates invalid code for extending dispatch method #3182

Closed
HannesWell opened this issue Aug 29, 2024 · 13 comments · Fixed by #3185
Closed

Xtend generates invalid code for extending dispatch method #3182

HannesWell opened this issue Aug 29, 2024 · 13 comments · Fixed by #3185

Comments

@HannesWell
Copy link
Contributor

HannesWell commented Aug 29, 2024

For an Xtend class that has a dispatch method, which 'extends' a dispatched method of a super-class, the generated code is faulty.
For example for

import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor

class MyInferrer extends AbstractModelInferrer {
	
	def dispatch void infer(Number element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
	}
}

the following code is generated

import java.util.Arrays;
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer;
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor;
import org.eclipse.xtext.xbase.lib.XbaseGenerated;

@SuppressWarnings("all")
public class MyInferrer extends AbstractModelInferrer {
  protected void _infer(final Number element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
  }

  @XbaseGenerated
  public void infer(final Number element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
    _infer(element, acceptor, isPreIndexingPhase);
    return; else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(element, acceptor, isPreIndexingPhase).toString());
    }
  }
}

The line return; else { causes the compiler-error Syntax error on token "else", delete this token.

@HannesWell HannesWell changed the title Xtend generated invalid code for extending dispatch method Xtend generates invalid code for extending dispatch method Aug 29, 2024
@cdietrich
Copy link
Contributor

@LorenzoBettini was this introduced with your recent change or is this older

@cdietrich
Copy link
Contributor

Maybe the problem is override a method with dispatch that is not a dispatch method in the superclass and thus there are no cases

@LorenzoBettini
Copy link
Contributor

@LorenzoBettini was this introduced with your recent change or is this older

I haven't changed the actual logic of generation.

@cdietrich
Copy link
Contributor

cdietrich commented Aug 30, 2024

hmm

class Viech extends AbstractModelInferrer {
	def dispatch void infer(Number element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
	}
}

gives me

public class Viech extends AbstractModelInferrer {
  protected void _infer(final Number element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
  }

  @XbaseGenerated
  public void infer(final EObject element, final IJvmDeclaredTypeAcceptor acceptor, final boolean isPreIndexingPhase) {
    if (element instanceof Number) {
      _infer((Number)element, acceptor, isPreIndexingPhase);
      return;
    } else if (element != null) {
      _infer(element, acceptor, isPreIndexingPhase);
      return;
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(element, acceptor, isPreIndexingPhase).toString());
    }
  }
}

=> maybe this is just a follow up, is there anything in the error log @HannesWell

org.eclipse.xtend.core.jvmmodel.DispatchMethodCompileStrategy

@HannesWell
Copy link
Contributor Author

=> maybe this is just a follow up, is there anything in the error log @HannesWell

org.eclipse.xtend.core.jvmmodel.DispatchMethodCompileStrategy

There is indeed the error below:
My first guess is therefore that the problem occurs because I use the latest Milestone/RC Eclipse 2024-09 Modelling Tools but the Target-Platform is the latest Release 2024-06 and therefore the new type org.eclipse.xtext.xbase.lib.XbaseGenerated is not (yet present).

java.lang.IllegalArgumentException: The type org.eclipse.xtext.xbase.lib.XbaseGenerated is not on the classpath.
	at org.eclipse.xtext.xbase.jvmmodel.JvmAnnotationReferenceBuilder.annotationRef(JvmAnnotationReferenceBuilder.java:84)
	at org.eclipse.xtext.xbase.jvmmodel.JvmAnnotationReferenceBuilder.annotationRef(JvmAnnotationReferenceBuilder.java:61)
	at org.eclipse.xtend.core.jvmmodel.XtendJvmModelInferrer.appendSyntheticDispatchMethods(XtendJvmModelInferrer.java:494)
	at org.eclipse.xtend.core.jvmmodel.XtendJvmModelInferrer.initialize(XtendJvmModelInferrer.java:410)
	at org.eclipse.xtend.core.jvmmodel.XtendJvmModelInferrer$5.run(XtendJvmModelInferrer.java:280)
	at org.eclipse.xtend.core.jvmmodel.XtendJvmModelInferrer$3.run(XtendJvmModelInferrer.java:220)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.ensureJvmMembersInitialized(BatchLinkableResource.java:232)
	at org.eclipse.xtext.common.types.impl.JvmDeclaredTypeImplCustom.checkPendingInitialization(JvmDeclaredTypeImplCustom.java:546)
	at org.eclipse.xtext.common.types.impl.JvmDeclaredTypeImplCustom.getSuperTypes(JvmDeclaredTypeImplCustom.java:595)
	at org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference.getSuperTypeByName(ParameterizedTypeReference.java:599)
	at org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference.getSuperTypeByName(ParameterizedTypeReference.java:566)
	at org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference.getSuperType(ParameterizedTypeReference.java:561)
	at org.eclipse.xtext.xbase.typesystem.references.ArrayTypes.doTryConvertToArray(ArrayTypes.java:86)
	at org.eclipse.xtext.xbase.typesystem.references.ArrayTypes.tryConvertToArray(ArrayTypes.java:38)
	at org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference.tryConvertToArray(ParameterizedTypeReference.java:900)
	at org.eclipse.xtext.xbase.typesystem.computation.SynonymTypesProvider.addArrayAndListSynonyms(SynonymTypesProvider.java:140)
	at org.eclipse.xtext.xbase.typesystem.computation.SynonymTypesProvider.collectSynonymTypes(SynonymTypesProvider.java:104)
	at org.eclipse.xtext.xbase.scoping.batch.FeatureScopes.createFeatureScopeForTypeRef(FeatureScopes.java:170)
	at org.eclipse.xtext.xbase.scoping.batch.FeatureScopes.createFeatureCallScopeForReceiver(FeatureScopes.java:241)
	at org.eclipse.xtext.xbase.scoping.batch.FeatureScopes.createFeatureCallScope(FeatureScopes.java:93)
	at org.eclipse.xtext.xbase.scoping.batch.AbstractFeatureScopeSession.createFeatureCallScope(AbstractFeatureScopeSession.java:178)
	at org.eclipse.xtext.xbase.scoping.batch.AbstractFeatureScopeSession.getScope(AbstractFeatureScopeSession.java:49)
	at org.eclipse.xtext.xbase.typesystem.internal.ScopeProviderAccess.getCandidateDescriptions(ScopeProviderAccess.java:143)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.getDescriptions(AbstractTypeComputationState.java:431)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.getLinkingCandidates(AbstractTypeComputationState.java:407)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer._computeTypes(XbaseTypeComputer.java:1227)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer.computeTypes(XbaseTypeComputer.java:113)
	at org.eclipse.xtext.xbase.annotations.typesystem.XbaseWithAnnotationsTypeComputer.computeTypes(XbaseWithAnnotationsTypeComputer.java:47)
	at org.eclipse.xtend.core.typesystem.XtendTypeComputer.computeTypes(XtendTypeComputer.java:65)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.doComputeTypes(AbstractTypeComputationState.java:124)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.computeTypes(AbstractTypeComputationState.java:112)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer._computeTypes(XbaseTypeComputer.java:572)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer.computeTypes(XbaseTypeComputer.java:153)
	at org.eclipse.xtext.xbase.annotations.typesystem.XbaseWithAnnotationsTypeComputer.computeTypes(XbaseWithAnnotationsTypeComputer.java:47)
	at org.eclipse.xtend.core.typesystem.XtendTypeComputer.computeTypes(XtendTypeComputer.java:65)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.doComputeTypes(AbstractTypeComputationState.java:124)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.computeTypes(AbstractTypeComputationState.java:112)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer._computeTypes(XbaseTypeComputer.java:484)
	at org.eclipse.xtext.xbase.typesystem.computation.XbaseTypeComputer.computeTypes(XbaseTypeComputer.java:119)
	at org.eclipse.xtext.xbase.annotations.typesystem.XbaseWithAnnotationsTypeComputer.computeTypes(XbaseWithAnnotationsTypeComputer.java:47)
	at org.eclipse.xtend.core.typesystem.XtendTypeComputer.computeTypes(XtendTypeComputer.java:65)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.doComputeTypes(AbstractTypeComputationState.java:124)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractTypeComputationState.computeTypes(AbstractTypeComputationState.java:112)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractRootTypeComputationState.computeTypes(AbstractRootTypeComputationState.java:33)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver._computeTypes(LogicalContainerAwareReentrantTypeResolver.java:852)
	at org.eclipse.xtend.core.typesystem.XtendReentrantTypeResolver._computeTypes(XtendReentrantTypeResolver.java:613)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver.computeTypes(LogicalContainerAwareReentrantTypeResolver.java:717)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver.computeMemberTypes(LogicalContainerAwareReentrantTypeResolver.java:922)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver._computeTypes(LogicalContainerAwareReentrantTypeResolver.java:911)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver.computeTypes(LogicalContainerAwareReentrantTypeResolver.java:711)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareReentrantTypeResolver.computeTypes(LogicalContainerAwareReentrantTypeResolver.java:703)
	at org.eclipse.xtend.core.typesystem.XtendReentrantTypeResolver.computeTypes(XtendReentrantTypeResolver.java:383)
	at org.eclipse.xtext.xbase.typesystem.internal.DefaultReentrantTypeResolver.resolve(DefaultReentrantTypeResolver.java:165)
	at org.eclipse.xtext.xbase.typesystem.internal.DefaultReentrantTypeResolver.reentrantResolve(DefaultReentrantTypeResolver.java:140)
	at org.eclipse.xtext.xbase.typesystem.internal.CompoundReentrantTypeResolver.reentrantResolve(CompoundReentrantTypeResolver.java:80)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$LazyResolvedTypes.resolveTypes(CachingBatchTypeResolver.java:81)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$2.process(CachingBatchTypeResolver.java:58)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$2.process(CachingBatchTypeResolver.java:1)
	at org.eclipse.xtext.util.concurrent.IUnitOfWork$Void.exec(IUnitOfWork.java:38)
	at org.eclipse.xtext.util.OnChangeEvictingCache.execWithoutCacheClear(OnChangeEvictingCache.java:145)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver.doResolveTypes(CachingBatchTypeResolver.java:54)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractBatchTypeResolver.resolveTypes(AbstractBatchTypeResolver.java:70)
	at org.eclipse.xtext.xbase.resource.BatchLinkingService.resolveBatched(BatchLinkingService.java:72)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.resolveLazyCrossReferences(BatchLinkableResource.java:166)
	at org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences(EcoreUtil2.java:505)
	at org.eclipse.xtext.builder.clustering.ClusteringBuilderState.doUpdate(ClusteringBuilderState.java:232)
	at org.eclipse.xtext.builder.builderState.AbstractBuilderState.update(AbstractBuilderState.java:171)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:522)
	at org.eclipse.xtext.builder.impl.XtextBuilder.addInfosFromTaskAndBuild(XtextBuilder.java:456)
	at org.eclipse.xtext.builder.impl.XtextBuilder.fullBuild(XtextBuilder.java:563)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:334)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:1077)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:296)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:352)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:441)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:444)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:555)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:503)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:585)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:207)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:300)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

@HannesWell
Copy link
Contributor Author

My first guess is therefore that the problem occurs because I use the latest Milestone/RC Eclipse 2024-09 Modelling Tools but the Target-Platform is the latest Release 2024-06 and therefore the new type org.eclipse.xtext.xbase.lib.XbaseGenerated is not (yet present).

I just verified that updating the target-platform solves this issue.

In order to allow working with a recent Xtext in the IDE on older Targets, I suggest to either wrap the problematic call to JvmAnnotationReferenceBuilder.annotationRef() in a try-catch block or to check in advance if the XbaseGenerated type is available:
https://github.com/eclipse/xtext/blob/197f6cc80038cb448115535fd5bc37440bf9f374/org.eclipse.xtend.core/src/org/eclipse/xtend/core/jvmmodel/XtendJvmModelInferrer.java#L494

If you want, I can provide a fix in the beginning of next week.

@szarnekow
Copy link
Contributor

szarnekow commented Aug 30, 2024

@LorenzoBettini IIRC there was a project preference that allows to skip that annotation.

Ah the check is at the right location indeed: https://github.com/eclipse/xtext/pull/3080/files#diff-e64c04b0476a37cb3aa3e8d5716a3472fc4f6294ad7070eb41105500790f6db7R493

@HannesWell It should work for you if you disable that in the preferences. Can you confirm this?

@LorenzoBettini
Copy link
Contributor

@szarnekow yes, that should help users in the same situation of @HannesWell, i.e., installing Xtend 2.36 but use an older TP.

However, a check should be implemented as @HannesWell suggested: we should check whether that annotation is in the classpath. IIRC, we do a similar check for other annotations, like the standard Java @Generated.

@cdietrich
Copy link
Contributor

see org.eclipse.xtext.xbase.jvmmodel.JvmModelCompleter.addAnnotations(JvmDeclaredType)

@HannesWell
Copy link
Contributor Author

Ah the check is at the right location indeed: https://github.com/eclipse/xtext/pull/3080/files#diff-e64c04b0476a37cb3aa3e8d5716a3472fc4f6294ad7070eb41105500790f6db7R493

@HannesWell It should work for you if you disable that in the preferences. Can you confirm this?

Yes I can confirm that unchecking the Annotate synthetic members with @XBaseGenerated box in the Xtend -> Compiler preferences make the error go away even when targeting older releases:
grafik

However, a check should be implemented as @HannesWell suggested: we should check whether that annotation is in the classpath. IIRC, we do a similar check for other annotations, like the standard Java @Generated.

Would you prefer to check the presense explicitly by for example wrapping the addition into
if (typeReferences.findDeclaredType(XbaseGenerated.class, operation) != null) or to just catch the IllegalArgumentException thrown if it is absent?

@cdietrich
Copy link
Contributor

+1 from me. @szarnekow @szarnekow wdyt?

@szarnekow
Copy link
Contributor

A proper guard condition sounds good to me. Please don’t catch the IAE

@HannesWell
Copy link
Contributor Author

A proper guard condition sounds good to me. Please don’t catch the IAE

Agree. I just tried to list all possibilities I could think of :)

Please have a look at #3185.

holgerfriedrich added a commit to holgerfriedrich/openhab-core that referenced this issue Oct 1, 2024
Workaround for issue in xtext code generator in xtext 2.36, see
eclipse-xtext/xtext#3182.

Signed-off-by: Holger Friedrich <[email protected]>
wborn pushed a commit to openhab/openhab-core that referenced this issue Oct 5, 2024
Workaround for issue in xtext code generator in xtext 2.36, see
eclipse-xtext/xtext#3182.

Signed-off-by: Holger Friedrich <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants