Skip to content

Commit

Permalink
Show a conflict warning
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jan 15, 2025
1 parent 3954e54 commit ccb92bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/main/java/net/starlark/java/eval/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.syntax.Location;

/** Debugger API. */
Expand All @@ -37,6 +38,29 @@ public interface Debugger {
void close();
}

/** A distinguished value that carries a message. */
@StarlarkBuiltin(
name = "debugger_message",
doc = "A distinguished value that carries a message.",
documented = false)
public static final class DebuggerMessage implements StarlarkValue {
private final String message;

public DebuggerMessage(String message) {
this.message = message;
}

@Override
public String toString() {
return "<" + message + ">";
}

@Override
public void repr(Printer printer) {
printer.append("<").append(message).append(">");
}
}

/** A Starlark value that can expose additional information to a debugger. */
public interface ValueWithDebugAttributes extends StarlarkValue {
/**
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/net/starlark/java/eval/StarlarkThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
Expand Down Expand Up @@ -182,20 +184,33 @@ public Location getLocation() {

@Override
public ImmutableMap<String, Object> getLocals() {
// TODO: List comprehensions introduce new locals that can shadow outer locals. Find a way to
// accurately represent them and their values in a situation such as:
//
// def foo(x):
// x += [[j(x) for x in i(x)] + h(x) for x in f(x) if g(x)]
// return k(x)
//
// TODO(adonovan): provide a more efficient API.
ImmutableMap.Builder<String, Object> env = ImmutableMap.builder();
LinkedHashMap<String, Object> env = new LinkedHashMap<>();
if (fn instanceof StarlarkFunction) {
for (int i = 0; i < locals.length; i++) {
Object local = locals[i];
if (local instanceof StarlarkFunction.Cell) {
local = ((StarlarkFunction.Cell) local).x;
}
if (local != null) {
env.put(((StarlarkFunction) fn).rfn.getLocals().get(i).getName(), local);
env.merge(
((StarlarkFunction) fn).rfn.getLocals().get(i).getName(),
local,
(oldValue, newValue) ->
Objects.equals(oldValue, newValue)
? oldValue
: new Debug.DebuggerMessage("different values in scope"));
}
}
}
return env.buildKeepingLast();
return ImmutableMap.copyOf(env);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def g(a, y, z):
.isEqualTo(
"""
<toplevel> @ main.star:5:2 local={}
g @ main.star:4:4 local={a=[0, 1, 2, 3, 4, 5], y=5, z=6, x=5}
g @ main.star:4:4 local={a=[0, 1, 2, 3, 4, 5], y=5, z=6, x=<different values in scope>}
f @ builtin:12 local={}
""");
}
Expand Down

0 comments on commit ccb92bd

Please sign in to comment.