Skip to content

Commit

Permalink
[lang] Fixing the copy of functions with generic parameters.
Browse files Browse the repository at this point in the history
see #720

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 28, 2017
1 parent 90e51f0 commit 09647eb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
Expand Up @@ -131,6 +131,8 @@
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices;
import org.eclipse.xtext.xbase.validation.ReadAndWriteTracking;
import org.eclipse.xtext.xtype.XFunctionTypeRef;
import org.eclipse.xtext.xtype.XtypeFactory;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.actionprototype.ActionParameterTypes;
Expand Down Expand Up @@ -3270,13 +3272,24 @@ protected JvmTypeReference cloneWithTypeParametersAndProxies(JvmTypeReference ty
boolean cloneType = true;
JvmTypeReference typeCandidate = type;

final List<JvmTypeParameter> typeParameters = forOperation.getTypeParameters();
// Use also cloneType as a flag that indicates if the type was already found in type parameters.
if (cloneType && type instanceof JvmParameterizedTypeReference) {
// Try to clone the type parameters.
final List<JvmTypeParameter> typeParameters = forOperation.getTypeParameters();
if (!typeParameters.isEmpty()) {
if (!typeParameters.isEmpty() && cloneType) {
if (type instanceof JvmParameterizedTypeReference) {
// Try to clone the type parameters.
cloneType = false;
typeCandidate = cloneAndAssociate(type, typeParameters);
} else if (type instanceof XFunctionTypeRef) {
// Try to clone the function reference.
final XFunctionTypeRef functionRef = (XFunctionTypeRef) type;
cloneType = false;
final XFunctionTypeRef cloneReference = XtypeFactory.eINSTANCE.createXFunctionTypeRef();
for (final JvmTypeReference paramType : functionRef.getParamTypes()) {
cloneReference.getParamTypes().add(cloneAndAssociate(paramType, typeParameters));
}
cloneReference.setReturnType(cloneAndAssociate(functionRef.getReturnType(), typeParameters));
cloneReference.setInstanceContext(functionRef.isInstanceContext());
typeCandidate = cloneReference;
}
}

Expand Down
Expand Up @@ -155,6 +155,46 @@ public class Bug720 extends AbstractSarlTest {
"}",
"");

private static final String SNIPSET4 = multilineString(
"package io.sarl.lang.tests.bug720",
"capacity ExampleCapacity",
"{",
" def exampleMethod(plan : (T) => void) with T",
"}");

private final String EXPECTED4 = multilineString(
"package io.sarl.lang.tests.bug720;",
"",
"import io.sarl.lang.annotation.SarlElementType;",
"import io.sarl.lang.annotation.SarlSpecification;",
"import io.sarl.lang.core.AgentTrait;",
"import io.sarl.lang.core.Capacity;",
"import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;",
"",
"@FunctionalInterface",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING+ "\")",
"@SarlElementType(" + SarlPackage.SARL_CAPACITY + ")",
"@SuppressWarnings(\"all\")",
"public interface ExampleCapacity extends Capacity {",
" public abstract <T extends Object> void exampleMethod(final Procedure1<? super T> plan);",
" ",
" public static class ContextAwareCapacityWrapper<C extends ExampleCapacity> extends Capacity.ContextAwareCapacityWrapper<C> implements ExampleCapacity {",
" public ContextAwareCapacityWrapper(final C capacity, final AgentTrait caller) {",
" super(capacity, caller);",
" }",
" ",
" public <T extends Object> void exampleMethod(final Procedure1<? super T> plan) {",
" try {",
" ensureCallerInLocalThread();",
" this.capacity.exampleMethod(plan);",
" } finally {",
" resetCallerInLocalThread();",
" }",
" }",
" }",
"}",
"");

@Inject
private CompilationTestHelper compiler;

Expand Down Expand Up @@ -203,4 +243,19 @@ public void compiling_03() throws Exception {
});
}

@Test
public void parsing_04() throws Exception {
SarlScript mas = file(SNIPSET4);
final Validator validator = validate(mas);
validator.assertNoErrors();
}

@Test
public void compiling_04() throws Exception {
this.compiler.compile(SNIPSET4, (it) -> {
final String actual = it.getGeneratedCode("io.sarl.lang.tests.bug720.ExampleCapacity");
assertEquals(EXPECTED4, actual);
});
}

}

0 comments on commit 09647eb

Please sign in to comment.