Skip to content

Commit

Permalink
Constructor parameters must be annotated
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Dec 23, 2024
1 parent 8f699f9 commit 51420df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@
* with {@link GenerateFields}. The parameter of the constructor can be one of the following:
*
* <ul>
* <li>Any reference, or primitive type without annotation
* <li>Any reference, or primitive type annotated with {@link IRField}
* <li>A subtype of {@code org.enso.compiler.ir.IR} annotated with {@link IRChild}
* <li>One of the <emph>meta</emph> types mentioned above
* </ul>
*
* Constructor parameters without any annotation will not be processed at all. There will be no code
* generated for them.
*
* <p>A user-defined field generated out of constructor parameter annotated with {@link IRChild} is
* a child element of this IR element. That means that it will be included in generated
* implementation of IR methods that iterate over the IR tree. For example {@code mapExpressions} or
Expand All @@ -47,6 +43,8 @@
*
* <p>For a constructor parameter of a meta type, there will be no user-defined field generated, as
* the meta fields are always generated.
*
* <p>Other types of constructor parameters are forbidden.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.CONSTRUCTOR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.CompilationSubject;
Expand Down Expand Up @@ -46,7 +47,7 @@ private static void expectCompilationFailure(String src) {
CompilationSubject.assertThat(compilation).failed();
}

private static Compilation compile(String src, String name) {
private static Compilation compile(String name, String src) {
var srcObject = JavaFileObjects.forSourceString(name, src);
var compiler = Compiler.javac().withProcessors(new IRProcessor());
var compilation = compiler.compile(srcObject);
Expand Down Expand Up @@ -194,6 +195,27 @@ public JName() {}
containsString("protected JNameGen()"));
}

@Test
public void annotatedConstructor_MustNotHaveUnannotatedParameters() {
var src =
"""
import org.enso.runtime.parser.dsl.GenerateIR;
import org.enso.runtime.parser.dsl.GenerateFields;
@GenerateIR
public final class JName {
@GenerateFields
public JName(int param) {}
}
""";
try {
compile("JName", src);
fail("Exception in compilation expected");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("must be annotated"));
}
}

@Test
public void annotatedConstructor_CanHaveMetaParameters() {
var src =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,21 @@ private void collectFromCtor() {
var irFieldAnnot = param.getAnnotation(IRField.class);
var irChildAnnot = param.getAnnotation(IRChild.class);
Field field;
if (Utils.hasNoAnnotations(param)) {
field = null;
} else if (irFieldAnnot != null) {
if (irFieldAnnot != null) {
field = processIrField(param, irFieldAnnot);
} else if (irChildAnnot != null) {
field = processIrChild(param, irChildAnnot);
} else {
var errMsg =
"Unexpected annotation on constructor parameter "
"Constructor parameter "
+ param
+ ". "
+ "All annotations: "
+ param.getAnnotationMirrors();
+ " must be annotated with either @IRField or @IRChild";
Utils.printError(errMsg, param, processingEnv.getMessager());
throw new IllegalStateException(errMsg);
}

if (field != null) {
fields.put(paramName, field);
}
assert field != null;
fields.put(paramName, field);
}
}

Expand Down

0 comments on commit 51420df

Please sign in to comment.