Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Macro for feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
yloiseau committed Oct 19, 2020
1 parent 3c1be40 commit 5b66895
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 21 deletions.
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Trying to reimplement the destructuring feature better (see #524 for a full intr
## TODO

- [x] implement the new desugarization
- [ ] allows to explicitly skip the rest (e.g. with special arg `_`) to avoid generating an unused (recursive) member
- [x] allows to explicitly skip the rest (e.g. with special arg `_`) to avoid generating an unused (recursive) member
- [x] Adds a custom exception to signal invalid destructuring
- update the known `destruct()` method in the stdlib
- provides a new destructuring method
Expand All @@ -22,8 +22,8 @@ Trying to reimplement the destructuring feature better (see #524 for a full intr
- [x] ranges
- [x] When IR
- [x] mark the old one deprecated (may depend on PR #551 to annotate golo code)
- [ ] use the real deprecated macro when #551 is merged
- [ ] provides augmentations to adapt both ways ?
- [x] use the real deprecated macro when #551 is merged
- [ ] ~~provides augmentations to adapt both ways ?~~
- [ ] update the doc
- [ ] describe new style destruct
- [ ] rationale
Expand Down
13 changes: 13 additions & 0 deletions src/main/golo/gololang/macros.golo
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,16 @@ macro deprecated = |args...| {
named: get("comment")?: value(),
positional)
}

----
Use old-style destructuring for the current module.
This macro customize the behavior of the destructuring feature by forcing the use of the `destruct` method instead of
`_$$_destruct`.
This is a toplevel macro.
----
@contextual
macro useOldstyleDestruct = |self| {
self: enclosingModule(): metadata("golo.destruct.newstyle", false)
}
12 changes: 10 additions & 2 deletions src/main/java/org/eclipse/golo/compiler/SugarExpansionVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.golo.compiler;

import gololang.ir.*;
import gololang.Messages;
import java.util.List;
import java.util.Deque;
import java.util.LinkedList;
Expand All @@ -30,7 +31,13 @@ class SugarExpansionVisitor extends AbstractGoloIrVisitor {
private final SymbolGenerator symbols = new SymbolGenerator("golo.compiler.sugar");
private final List<GoloFunction> functionsToAdd = new LinkedList<>();
private GoloModule module;
private final boolean useNewStyleDestruct = gololang.Runtime.loadBoolean("new", "golo.destruct.version", "GOLO_DESTRUCT_VERSION", true);

private boolean useNewStyleDestruct() {
if (this.module.metadata("golo.destruct.newstyle") != null) {
return (boolean) this.module.metadata("golo.destruct.newstyle");
}
return gololang.Runtime.loadBoolean("true", "golo.destruct.newstyle", "GOLO_DESTRUCT_NEWSTYLE", true);
}

@Override
public void visitModule(GoloModule module) {
Expand Down Expand Up @@ -380,7 +387,7 @@ public void visitForEachLoopStatement(ForEachLoopStatement foreachStatement) {
*/
@Override
public void visitDestructuringAssignment(DestructuringAssignment assignment) {
Block replacement = useNewStyleDestruct ? newDestructuring(assignment) : oldDestructuring(assignment);
Block replacement = useNewStyleDestruct() ? newDestructuring(assignment) : oldDestructuring(assignment);
assignment.replaceInParentBy(replacement);
replacement.accept(this);
}
Expand All @@ -402,6 +409,7 @@ public void visitDestructuringAssignment(DestructuringAssignment assignment) {
* </code></pre>
*/
private Block oldDestructuring(DestructuringAssignment assignment) {
Messages.warning(Messages.message("oldstyle_destruct", this.module.getPackageAndClass()), assignment);
LocalReference tmpRef = LocalReference.of(symbols.next("destruct")).synthetic();
Block block = Block.of(AssignmentStatement.create(tmpRef, invoke("destruct").on(assignment.expression()), true));
int last = assignment.getReferencesCount() - 1;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ no_parameter_names = The function `{0}` has no parameter names but is called wit
unavailable_class = `{0}` used in `{1}` can\u2019t be loaded.\n\tSee <{2}#warning-unavailable-class> for more information.

deprecated_element = `{0}` in `{1}` is deprecated.\n\tSee <{2}#warning-deprecated> for more information.
oldstyle_destruct = `{0}` uses old-style destructuring, which is deprecated.

# Documentation warnings and errors ===========================================
multiple_package_desc = Multiple description files found for package `{0}`; using the first one.\n\tSee <{1}#warning-multiple-package-desc> for more information.
1 change: 1 addition & 0 deletions src/main/resources/messages_fr_FR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct_private_member = Membre priv\u00e9 de `{0}`
no_parameter_names = La fonction `{0}` n\u2019a pas de param\u00e8tre nomm\u00e9 mais est appel\u00e9e avec {1} comme noms d\u2019arguments.\n\tVoir <{2}#warning-no-parameter-names> pour plus d\u2019informations.
unavailable_class = `{0}` utilis\u00e9e dans `{1}` ne peut pas \u00eatre charg\u00e9e.\n\tVoir <{2}#warning-unavailable-class> pour plus d\u2019informations.
deprecated_element = `{0}` dans `{1}` est obsol\u00e8te.\n\tVoir <{2}#warning-deprecated> pour plus d\u2019informations.
oldstyle_destruct = `{0}` utilise l\u2019ancienne version de la d\u00e9structuration qui est obsol\u00e8te.

# Documentation warnings and errors ===========================================
multiple_package_desc = Fichiers de description surnum\u00e9raires pour le paquet `{0}`; utilisation du premier.\n\tVoir <{1}#warning-multiple-package-desc> pour plus d\u2019informations.
Expand Down
11 changes: 2 additions & 9 deletions src/test/java/org/eclipse/golo/compiler/CompileAndRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1771,15 +1771,8 @@ public void destructuring() throws Throwable {
if (bootstraping()) {
return;
}
Class<?> moduleClass = compileAndLoadGoloModule(SRC, "destruct.golo");
for (Method testMethod : getTestMethods(moduleClass)) {
try {
testMethod.invoke(null);
} catch (InvocationTargetException e) {
fail("method " + testMethod.getName() + " in " + SRC + "destruct.golo failed: " +
e.getCause());
}
}
runTests(SRC, "destruct.golo", classLoader(this));
runTests(SRC, "destruct-old.golo", classLoader(this));
}

@Test
Expand Down
25 changes: 25 additions & 0 deletions src/test/resources/for-execution/destruct-old.golo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module golotest.execution.DestructuringOld

import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers

import org.eclipse.golo.runtime

&useOldstyleDestruct()

function test_tuple_old = {
let fst, scd = [1, 2, 3, 4]
assertThat(fst, `is(1))
assertThat(scd, `is(2))
}

function test_list_old = {
let a, b... = list[1, 2, 3, 4]
assertThat(a, `is(1))
assertThat(b, `is(tuple[2, 3, 4]))
}

function main = |args| {
test_tuple_old()
test_list_old()
}
7 changes: 0 additions & 7 deletions src/test/resources/for-execution/destruct.golo
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ function test_tuple_rest = {
assertThat(c, `is([3, 4, 5]))
}

# ignored, old version. Should fail now.
function _tuple_less_old = {
let fst, scd = [1, 2, 3, 4]
require(fst == 1, "err")
require(scd == 2, "err")
}

function test_tuple_less_new = {
try {
let fst, scd = [1, 2, 3, 4]
Expand Down

0 comments on commit 5b66895

Please sign in to comment.