Class Java8InferenceContext
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal DeclaredTypeThe type of class that encloses the top level expression whose type arguments are inferred.final ProcessingEnvironmentjavax.annotation.processing.ProcessingEnvironmentfinal TypeMirrorTypeMirror for java.lang.Error.final InvocationTypeInferenceInvocation type inference object.final InferenceFactoryThe inference factory.final Set<VariableElement> There's no way to tell if an element is a parameter of a lambda, so keep track of them.final IdentityHashMap<ExpressionTree, Theta> Store previously created type variable to inference variable maps as a map from invocation expression to Theta.static final intMaximum amount of work permitted for a single inference problem before inference is abandoned.final Typesjavax.lang.model.util.Typesfinal ProperTypeProperType for java.lang.Object.Path to the top level expression whose type arguments are inferred.final TypeMirrorTypeMirror for java.lang.RuntimeException.final AnnotatedTypeFactoryThe annotated type factory.final com.sun.tools.javac.code.Typescom.sun.tools.javac.code.Types -
Constructor Summary
ConstructorsConstructorDescriptionJava8InferenceContext(AnnotatedTypeFactory factory, TreePath pathToExpression, InvocationTypeInference inference) Creates a context. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddLambdaParms(List<? extends VariableTree> parameters) Adds the parameters to the list of trees that are lambda parameters.intReturns the next number to use as the id for a capture variable.intReturns the next number to use as the id for a qualifier variable.intReturns the next number to use as the id for a non-capture variable.booleanisLambdaParam(ExpressionTree expression) Returns true if theexpressionis a lambda parameter.voidrecordIncorporationWork(int amount) Recordsamountunits of bound-incorporation work for this inference problem and throwsInferenceBudgetExceededErrorif the total exceedsMAX_INCORPORATION_WORK.inttypeSize(AnnotatedTypeMirror type) Returns the number of components oftype, an estimate of the cost of copying or substituting into it.
-
Field Details
-
pathToExpression
Path to the top level expression whose type arguments are inferred. -
env
javax.annotation.processing.ProcessingEnvironment -
object
ProperType for java.lang.Object. -
inference
Invocation type inference object. -
types
public final com.sun.tools.javac.code.Types typescom.sun.tools.javac.code.Types -
modelTypes
javax.lang.model.util.Types -
enclosingType
The type of class that encloses the top level expression whose type arguments are inferred. -
maps
Store previously created type variable to inference variable maps as a map from invocation expression to Theta. -
runtimeEx
TypeMirror for java.lang.RuntimeException. -
error
TypeMirror for java.lang.Error. -
inferenceTypeFactory
The inference factory. -
typeFactory
The annotated type factory. -
lambdaParms
There's no way to tell if an element is a parameter of a lambda, so keep track of them. -
MAX_INCORPORATION_WORK
public static final int MAX_INCORPORATION_WORKMaximum amount of work permitted for a single inference problem before inference is abandoned. This totals work from all three phases of JLS 18 thatrecordIncorporationWork(int)is charged against: bound incorporation to a fixed point (JLS 18.3, the sum of bound-list sizes visited byVariableBounds.applyInstantiationsToBounds(), including its already-resolved fast path, which still applies constraints and so is charged for the size of the constraint set), variable resolution (JLS 18.4, charged per attempt inResolution.resolveSmallestSet(java.util.Set<org.checkerframework.framework.util.typeinference8.types.Variable>, org.checkerframework.framework.util.typeinference8.bound.BoundSet)for the size of the variable set being resolved), and substitution of a resolved instantiation back into a type's structure (InferenceType.applyInstantiations(), charged both for the number of inference variables and for the size of the instantiations being substituted).Incorporating bounds to a fixed point is roughly cubic in the nesting depth of a generic invocation, and resolving many mutually dependent inference variables together (e.g. many mutually F-bounded type parameters resolved by one wildcarded generic invocation) is worse: there each variable's instantiation embeds a copy of every earlier one, so the instantiations double in size with each variable resolved. So a single pathological invocation (often machine-generated, but see
fboundin.claude/skills/cf-performance/gen-shapes.pyfor a hand-written shape that reaches it) can otherwise take many seconds or effectively hang the compiler. This bound caps that work: when it is exceeded anInferenceBudgetExceededErroris thrown and inference falls back to a sound, conservative result. The largest work measured on real code is 1941 units (Guava, with the Nullness Checker) and 2086 units over the whole:checker:testcorpus; this value keeps roughly five times that headroom, so it does not affect realistic programs while still abandoning a pathological invocation after at most a few seconds. Override with-AinferenceWorkBudget=N(raise it for legitimate machine-generated code that hits it).- See Also:
-
-
Constructor Details
-
Java8InferenceContext
public Java8InferenceContext(AnnotatedTypeFactory factory, TreePath pathToExpression, InvocationTypeInference inference) Creates a context.- Parameters:
factory- type factorypathToExpression- path to the expression whose type arguments are inferredinference- inference object
-
-
Method Details
-
typeSize
Returns the number of components oftype, an estimate of the cost of copying or substituting into it.- Parameters:
type- the type to measure- Returns:
- the number of components of
type
-
recordIncorporationWork
public void recordIncorporationWork(int amount) Recordsamountunits of bound-incorporation work for this inference problem and throwsInferenceBudgetExceededErrorif the total exceedsMAX_INCORPORATION_WORK.- Parameters:
amount- amount of work to add to this problem's running total
-
getNextVariableId
public int getNextVariableId()Returns the next number to use as the id for a non-capture variable. This id is only unique for this inference problem.- Returns:
- the next number to use as the id for a non-capture variable
-
getNextCaptureVariableId
public int getNextCaptureVariableId()Returns the next number to use as the id for a capture variable. This id is only unique for this inference problem.- Returns:
- the next number to use as the id for a capture variable
-
getNextQualifierVariableId
public int getNextQualifierVariableId()Returns the next number to use as the id for a qualifier variable. This id is only unique for this inference problem.- Returns:
- the next number to use as the id for a qualifier variable
-
addLambdaParms
Adds the parameters to the list of trees that are lambda parameters.There's no way to tell if a tree is a parameter of a lambda, so keep track of them.
- Parameters:
parameters- list of lambda parameters
-
isLambdaParam
Returns true if theexpressionis a lambda parameter.- Parameters:
expression- an expression- Returns:
- true if the
expressionis a lambda parameter
-