typetools / typetools/checker-framework

No local inference for array component types

Open
#3,179 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Array component types on the left-hand side of assignments are not inferred, even though inference succeeds for the right-hand side. This happens for all type systems; my examples use the Index Checker (to distinguish this from #599). Consider the following program:

// Test local inference of array constant types

import org.checkerframework.checker.index.qual.*;

class ArrayInfer {
    void testIndex() {
	// I expect nn_annotated and nn_unannotated to have the same
	// (inferred) type, because they're both effectively final
	// and both have the same initializer.
	@NonNegative int[] nn_annotated = new int[] { 0, 5, 3 };
	int[] nn_unannotated = new int[] { 0, 5, 3 };

	// OK
	@NonNegative int[] x = nn_annotated;

	// unexpected error here
	@NonNegative int[] y = nn_unannotated;
    }
}

Both arrays are effectively final and have the same initializer. I would therefore expect local type inference to infer that the component type of nn_unannotated is @NonNegative. However, running javacheck -processor index ArrayInfer.java yields:

ArrayInfer.java:17: error: [assignment.type.incompatible] incompatible types in assignment.
	@NonNegative int[] y = nn_unannotated;
	                       ^
  found   : @LowerBoundUnknown int @LowerBoundUnknown []
  required: @NonNegative int @LowerBoundUnknown []
1 error

By contrast, if I change the code so that this is 3 (unannotated) integers instead of a 3-element integer array, the types are inferred as expected.

I think the problem here is that local type inference doesn't attempt to update the component type, even if the variable is effectively final and there is an initializer.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ArrayInfer.java example and run javacheck -processor index ArrayInfer.java to reproduce the component-type mismatch. Trace local type inference for effectively final array variables and compare it with the working scalar case. Done means the unannotated array's component type is inferred from its initializer and the assignment passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.