Skip to content

Commit

Permalink
[lang] Generate an error when a generic type parameter is hiding anot…
Browse files Browse the repository at this point in the history
…her generic type parameter.

close #1021

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jul 17, 2020
1 parent 1a40f90 commit 7e2f2bd
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The main features coming from the Java language are supported by SARL too. The f
differences between the SARL, Java, Xtend and Scala languages, excluding any feature provided by the development
environment (Eclipse, IntelliJ...)


<table><thead>
<tr><th></th><th>SARL</th><th>Java</th><th>Xtend</th><th>Scala</th></tr>
</thead><tbody>
Expand Down Expand Up @@ -156,6 +157,7 @@ environment (Eclipse, IntelliJ...)
</tbody></table>



## References

This documentation is based on elements from the following sources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,13 @@
"message": "*any-message*",
"cause": "This error is generated when your SARL code has an improper syntax, and cannot be parsed by the SARL compiler. The error message provides the detail of the invalid syntax and the position into your source code that is under failure",
"level": "error"
},
{
"code": "io.sarl.lang.validation.IssueCodes.generic_type_name_shadowing",
"message": "The generic type parameter '*name*' is hiding the generic type parameter of '*type-name*'",
"cause": "This error is generated you declared a generic type parameter for an action that has the same name as another generic type parameter that is declared into the enclosing type (class, or interface)",
"solution": "Change the *name*",
"level": "error"
}
]

Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ public final class IssueCodes {
public static final String POTENTIAL_INEFFICIENT_VALUE_CONVERSION =
ISSUE_CODE_PREFIX + "potential_inefficient_value_conversion"; //$NON-NLS-1$

/**
* A generic type name is shadowing another generic type name.
* @since 0.12
*/
public static final String GENERIC_TYPE_NAME_SHADOWING =
ISSUE_CODE_PREFIX + "generic_type_name_shadowing"; //$NON-NLS-1$

private IssueCodes() {
//
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private Messages() {
public static String SARLValidator_47;
public static String SARLValidator_48;
public static String SARLValidator_49;
public static String SARLValidator_5;
public static String SARLValidator_50;
public static String SARLValidator_51;
public static String SARLValidator_52;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static io.sarl.lang.validation.IssueCodes.DISCOURAGED_FUNCTION_NAME;
import static io.sarl.lang.validation.IssueCodes.DISCOURAGED_LOOP_BREAKING_KEYWORD_USE;
import static io.sarl.lang.validation.IssueCodes.DISCOURAGED_OCCURRENCE_READONLY_USE;
import static io.sarl.lang.validation.IssueCodes.GENERIC_TYPE_NAME_SHADOWING;
import static io.sarl.lang.validation.IssueCodes.INVALID_CAPACITY_TYPE;
import static io.sarl.lang.validation.IssueCodes.INVALID_DEFAULT_SKILL_ANNOTATION;
import static io.sarl.lang.validation.IssueCodes.INVALID_EXTENDED_TYPE;
Expand Down Expand Up @@ -170,8 +171,10 @@
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.common.types.JvmStringAnnotationValue;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeParameter;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.JvmVisibility;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
Expand Down Expand Up @@ -1304,6 +1307,39 @@ private boolean isReallyDisallowedName(QualifiedName name) {
return false;
}

/** Check if the given generic type has a name that is shadowing an enclosing generic type.
*
* @param type the generic type parameter to check.
* @since 0.12
*/
@Check
public void checkGenericTypeNameShadowing(JvmTypeParameter type) {
final XtendMember declarator = EcoreUtil2.getContainerOfType(type.eContainer(), XtendMember.class);
if (declarator instanceof XtendFunction && !Utils.isHiddenMember(type.getName())) {
final XtendTypeDeclaration enclosingType = declarator.getDeclaringType();
List<JvmTypeParameter> params = null;
if (enclosingType instanceof XtendClass) {
params = ((XtendClass) enclosingType).getTypeParameters();
} else if (enclosingType instanceof XtendInterface) {
params = ((XtendInterface) enclosingType).getTypeParameters();
}
if (params != null && !params.isEmpty()) {
// Do not need to loop on the enclosing types since all the inner types must be declared as static
for (final JvmTypeParameter declaredType : params) {
if (Strings.equal(type.getSimpleName(), declaredType.getSimpleName())) {
error(
MessageFormat.format(Messages.SARLValidator_5, type.getSimpleName(), enclosingType.getName()),
type,
TypesPackage.Literals.JVM_TYPE_PARAMETER__NAME,
ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
GENERIC_TYPE_NAME_SHADOWING);
return;
}
}
}
}
}

/** Check if the given action has a valid name.
*
* @param action the action to test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SARLValidator_46=Expecting the return type {0}. It is recommended to write the r
SARLValidator_47=The method {0} of type {1} must use override keyword since it actually overrides a supertype method.
SARLValidator_48=The method {0} of type {1} shadows the method {2} of type {3}, but does not override it.
SARLValidator_49=override
SARLValidator_5=The generic type parameter ''{0}'' is hiding the generic type parameter of ''{1}''.
SARLValidator_50=Duplicate implemented feature ''{0}''.
SARLValidator_51=Unexpected assertion due to its positive test result.
SARLValidator_52=The feature ''{0}'' is already implemented by the super-type ''{1}''.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ abstract class AbstractBehaviorGuardEvaluatorRegistry<T> implements IBehaviorGua
}

@Pure
override getRegisteredEventListeners(type : Class<T>) : ConcurrentSkipListSet<T> with T {
var result : ConcurrentSkipListSet<T> = new ConcurrentSkipListSet(ObjectComparator::SINGLETON)
override getRegisteredEventListeners(type : Class<TT>) : ConcurrentSkipListSet<TT> with TT {
var result : ConcurrentSkipListSet<TT> = new ConcurrentSkipListSet(ObjectComparator::SINGLETON)
getRegisteredEventListeners(type, result)
return result
}
Expand Down

0 comments on commit 7e2f2bd

Please sign in to comment.