[BUG] Bracket literal infers Array and cannot be passed to a List parameter; list.mojo docstrings label it List
Nobody has claimed this yet.
- Dominant language
- Mojo
- Stars
- 29.8k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Bug description
Actual behavior
A bracket literal infers Array[Int, N], which does not convert to List[Int], so it cannot be passed to a List parameter:
def take(x: List[Int]):
print(len(x))
def main():
var v = [1, 2, 3]
take(v)
error: invalid call to 'take': value passed to 'x' cannot be converted from 'Array[Int, Int(3)]' to 'List[Int]'
take(v)
~~~~ ^
collections/list.mojo's own docstrings still document the opposite. For example:
var int_list = [1, 2, 3] # List[Int]
var str_list = ["a", "b", "c"] # List[String]
Those # List[Int] annotations are now incorrect — the inferred type is Array. There are no uses of the explicitly-annotated var x: List[Int] = [1, 2, 3] form in that file, so every bracket-literal example in the List docs mis-states the type it produces.
Expected behavior
Either the literal should still be usable where a List is expected, or the List documentation should be corrected to show the annotated form (var x: List[Int] = [1, 2, 3]) and to stop labeling bare literals as List. Right now the documented behavior and the actual behavior disagree, which makes the List docs actively misleading for anyone following them.
Steps to reproduce
Save the first snippet as literal_bug.mojo and run mojo literal_bug.mojo.
System information
Reproduces at the current head of main (built from source) on macOS (Apple Silicon). Platform-independent.
CC: @bgreni
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the bracket-literal examples in collections/list.mojo and reproduce the issue with the literal_bug.mojo snippet using mojo literal_bug.mojo. Determine whether the intended resolution is to restore List usability or correct the documentation to show the annotated form. Done means the documented type and actual behavior no longer disagree.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers, documentation
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100