Skip to content

Commit

Permalink
[Skylark] Control whether range type is used with --incompatible_rang…
Browse files Browse the repository at this point in the history
…e_type.
  • Loading branch information
ttsugriy committed Jul 16, 2018
1 parent 0c25813 commit aa7d616
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 230 deletions.
9 changes: 9 additions & 0 deletions site/docs/skylark/backward-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ no user-visible impact.
* Default: `true`


### Python 3 range behavior.
When set, the result of `range(...)` function is a lazy `range` type instead of
a `list`. Because of this repetitions using `*` operator are no longer
supported and `range` slices are also lazy `range` instances.

* Flag: `--incompatible_range_type`
* Default: `false`


### Disable objc provider resources

This flag disables certain deprecated resource fields on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void serialize(
codedOut.writeBoolNoTag(semantics.incompatibleNewActionsApi());
codedOut.writeBoolNoTag(semantics.incompatibleNoSupportToolsInActionInputs());
codedOut.writeBoolNoTag(semantics.incompatiblePackageNameIsAFunction());
codedOut.writeBoolNoTag(semantics.incompatibleRangeType());
codedOut.writeBoolNoTag(semantics.incompatibleRemoveNativeGitRepository());
codedOut.writeBoolNoTag(semantics.incompatibleRemoveNativeHttpArchive());
codedOut.writeBoolNoTag(semantics.incompatibleStringIsNotIterable());
Expand Down Expand Up @@ -85,6 +86,7 @@ public SkylarkSemantics deserialize(DeserializationContext context, CodedInputSt
builder.incompatibleNewActionsApi(codedIn.readBool());
builder.incompatibleNoSupportToolsInActionInputs(codedIn.readBool());
builder.incompatiblePackageNameIsAFunction(codedIn.readBool());
builder.incompatibleRangeType(codedIn.readBool());
builder.incompatibleRemoveNativeGitRepository(codedIn.readBool());
builder.incompatibleRemoveNativeHttpArchive(codedIn.readBool());
builder.incompatibleStringIsNotIterable(codedIn.readBool());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ public class SkylarkSemanticsOptions extends OptionsBase implements Serializable
)
public boolean incompatiblePackageNameIsAFunction;

@Option(
name = "incompatible_range_type",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help = "If set to true, range() will use the 'range' type instead of 'list'."
)
public boolean incompatibleRangeType;

@Option(
name = "incompatible_remove_native_git_repository",
defaultValue = "false",
Expand Down Expand Up @@ -338,6 +351,7 @@ public SkylarkSemantics toSkylarkSemantics() {
.incompatibleNewActionsApi(incompatibleNewActionsApi)
.incompatibleNoSupportToolsInActionInputs(incompatibleNoSupportToolsInActionInputs)
.incompatiblePackageNameIsAFunction(incompatiblePackageNameIsAFunction)
.incompatibleRangeType(incompatibleRangeType)
.incompatibleRemoveNativeGitRepository(incompatibleRemoveNativeGitRepository)
.incompatibleRemoveNativeHttpArchive(incompatibleRemoveNativeHttpArchive)
.incompatibleStringIsNotIterable(incompatibleStringIsNotIterable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.syntax.Concatable.Concatter;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableListLike;
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.SkylarkList.TupleLike;
import java.io.IOException;
import java.util.IllegalFormatException;

Expand Down Expand Up @@ -308,15 +306,6 @@ private static Object plus(
}
}

if (lval instanceof MutableListLike && rval instanceof MutableListLike) {
return MutableList.concat(((MutableListLike<?>) lval).toMutableList(),
((MutableListLike<?>) rval).toMutableList(), env.mutability());
}

if (lval instanceof TupleLike && rval instanceof TupleLike) {
return Tuple.concat(((TupleLike<?>) lval).toTuple(), ((TupleLike<?>) rval).toTuple());
}

if (lval instanceof SkylarkDict && rval instanceof SkylarkDict) {
if (env.getSemantics().incompatibleDisallowDictPlus()) {
throw new EvalException(
Expand Down Expand Up @@ -399,7 +388,7 @@ private static Object mult(Object lval, Object rval, Environment env, Location l
} else if (otherFactor instanceof String) {
// Similar to Python, a factor < 1 leads to an empty string.
return Strings.repeat((String) otherFactor, Math.max(0, number));
} else if (otherFactor instanceof SkylarkList) {
} else if (otherFactor instanceof SkylarkList && !(otherFactor instanceof RangeList)) {
// Similar to Python, a factor < 1 leads to an empty string.
return ((SkylarkList<?>) otherFactor).repeat(number, env.mutability());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature;
import com.google.devtools.build.lib.syntax.EvalUtils.ComparisonException;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import com.google.devtools.build.lib.syntax.SkylarkList.RangeList;
import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
import com.google.devtools.build.lib.syntax.Type.ConversionException;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -622,7 +621,7 @@ public Integer invoke(String value) throws EvalException {

@SkylarkSignature(
name = "range",
returnType = RangeList.class,
returnType = SkylarkList.class,
doc =
"Creates a list where items go from <code>start</code> to <code>stop</code>, using a "
+ "<code>step</code> increment. If a single argument is provided, items will "
Expand Down Expand Up @@ -659,7 +658,7 @@ public Integer invoke(String value) throws EvalException {
)
private static final BuiltinFunction range =
new BuiltinFunction("range") {
public RangeList invoke(
public SkylarkList<Integer> invoke(
Integer startOrStop, Object stopOrNone, Integer step, Location loc, Environment env)
throws EvalException {
int start;
Expand All @@ -674,12 +673,8 @@ public RangeList invoke(
if (step == 0) {
throw new EvalException(loc, "step cannot be 0");
}
if (step > 0) {
// simplify handling of a case like range(-2) or its equivalent range(-2, 0)
// by turning it into range(0, 0)
stop = Math.max(start, stop);
}
return RangeList.of(start, stop, step);
RangeList range = RangeList.of(start, stop, step);
return env.getSemantics().incompatibleRangeType() ? range : range.toMutableList(env);
}
};

Expand Down
Loading

0 comments on commit aa7d616

Please sign in to comment.