JetBrains/intellij-community · error · SliceFilterParseException
slice.filter.parse.error.expression.must.evaluate.to.constant
slice.filter.parse.error.expression.must.evaluate.to.constant
Error message
Expression must evaluate to a constant: {0} What it means
Thrown by JavaSliceProvider.parseFilter when the filter expression is syntactically valid and type-compatible but does not fold to a compile-time constant. ExpressionUtils.computeConstantExpression(constant) returning null (no constant value) triggers SliceFilterParseException; slice value filters only support dataflow-tractable constants.
Source
Thrown at java/java-impl/src/com/intellij/slicer/JavaSliceProvider.java:170
return new JavaValueFilter(DfTypes.referenceConstant(enumConstant, type));
} else {
throw new SliceFilterParseException(JavaBundle.message("slice.filter.parse.error.enum.constant.not.found", filter));
}
}
PsiExpression constant;
try {
constant = JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText(filter, expression);
}
catch (IncorrectOperationException ignore) {
throw new SliceFilterParseException(JavaBundle.message("slice.filter.parse.error.incorrect.expression", filter));
}
PsiType constantType = constant.getType();
if (constantType == null || !type.isAssignableFrom(constantType)) {
throw new SliceFilterParseException(JavaBundle.message("slice.filter.parse.error.incorrect.constant.type", type.getPresentableText()));
}
Object o = ExpressionUtils.computeConstantExpression(constant);
if (o == null) {
throw new SliceFilterParseException(JavaBundle.message("slice.filter.parse.error.expression.must.evaluate.to.constant", filter));
}
if (relationType != RelationType.EQ) {
if (!(o instanceof Number)) {
throw new SliceFilterParseException(JavaBundle.message("slice.filter.parse.error.incorrect.constant.expected.number", filter));
}
if (PsiTypes.longType().equals(type)) {
LongRangeSet rangeSet = LongRangeSet.point(((Number)o).longValue()).fromRelation(relationType);
return new JavaValueFilter(DfTypes.longRange(rangeSet));
}
LongRangeSet rangeSet = LongRangeSet.point(((Number)o).intValue()).fromRelation(relationType);
return new JavaValueFilter(DfTypes.intRangeClamped(rangeSet));
}
return new JavaValueFilter(DfTypes.constant(o, type));
}
private static @Nullable PsiType getType(@NotNull PsiElement expression) {
if (expression instanceof PsiExpression) {
return ((PsiExpression)expression).getType();View on GitHub (pinned to be881553f2)
Solutions
- Use a literal or a compile-time constant expression (e.g. '42', '"abc"', 'true').
- For enum-typed values use the bare enum constant name, which takes the dedicated enum branch and needs no evaluation.
- Replace 'new X(...)' or method-call filters with the concrete value you care about.
- Remember the filter describes a dataflow fact, not a predicate evaluated at runtime.
Example fix
// before: filter = "System.currentTimeMillis()" // after: filter = "1000L"
Defensive patterns
Strategy: validation
Validate before calling
Object v = ExpressionUtils.computeConstantExpression(candidate);
if (v == null) {
// filter expression does not fold to a constant — reject with guidance
} Try / catch
try { provider.parseFilter(expression, filter); } catch (SliceFilterParseException e) { /* ask user for a constant expression */ } Prevention
- Restrict filter suggestions to literals and compile-time constants.
- Do not expect runtime evaluation in slice filters.
When it happens
Trigger: Entering a filter like 'Math.random()', 'System.currentTimeMillis()', 'new Date()', or a reference to a non-final/non-constant local variable — valid Java, right type, but no compile-time constant value.
Common situations: Users expecting runtime evaluation semantics from the filter; filtering on static final fields that are not compile-time constants (e.g. initialized with a method call); filtering on enum-adjacent static fields instead of enum constants.
Related errors
- slice.filter.parse.error.not.null.filter.not.applicable.for.primitive.type
- slice.filter.parse.error.enum.constant.not.found
- slice.filter.parse.error.incorrect.expression
- slice.filter.parse.error.incorrect.constant.type
- slice.filter.parse.error.incorrect.constant.expected.number
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/c72f04a868c62a12.
Report an issue: GitHub.