Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message does not indicate conflicting type parameters #42401

Closed
tyleringebrand opened this issue Sep 27, 2021 · 4 comments · Fixed by #42422
Closed

Error message does not indicate conflicting type parameters #42401

tyleringebrand opened this issue Sep 27, 2021 · 4 comments · Fixed by #42422
Labels
error messages Better, more actionable error messages

Comments

@tyleringebrand
Copy link

Originally found here, the issue is a error message for incorrect code is very misleading. Here is a minimum working example:

struct X{T, A<:AbstractArray{T}}
    t::T
    a::A
end

Vector{X{Int, Vector{Float64}}}(undef, 2)

The issue is that Float64 does not match Int, which causes an error. However, the error message is:

ERROR: LoadError: TypeError: in X, in A, expected A<:(AbstractArray{T, N} where N), got Type{Vector{Float64}}
Stacktrace:
 [1] top-level scope
   @ c:\Users\Tyler\Documents\Julia\Learning\test.jl:6
in expression starting at c:\Users\Tyler\Documents\Julia\Learning\test.jl:6

The reason why this is confusing is because it says the issue is with A being of type AbstractArray{T, N} and that the actual type is Vector{Float64}. Vector is of type AbstractArray, and Float64 is a type. Therefore, I could not figure out why this is wrong because it does seem like the type of A given, Vector{Float64}, is a valid type of A, AbstractArray{T}.
It would be nice if the error message specified that the given Types were wrong because Float64 does not match Int and therefore T is being redefined.

Note in this example it may seem obvious, but this can become quite confusing for a more complicated struct.

If version is relevant:

Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)      
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 
@zsunberg
Copy link
Contributor

@tyleringebrand you may want to make the title more informative, e.g. "Error message does not indicate conflicting type parameters" (I would change it myself but I don't have sufficient permission)

@zsunberg
Copy link
Contributor

@tyleringebrand , it would also be nice to test this with version 1.7 to make sure it hasn't been fixed already.

@zsunberg
Copy link
Contributor

To perhaps make it more clear why this is confusing, consider:

struct X{T<:Real, A<:AbstractArray{T}}
    t::T
    a::A
end

Vector{X{Int, Vector{Float64}}}(undef, 2)

which throws

ERROR: TypeError: in X, in A, expected A<:(AbstractArray{T<:Real, N} where N), got Type{Vector{Float64}}

Vector{Float64} is a subtype of AbstractArray{<:Real, N} where N, but the code will not run because T actually has more constraints than just <:Real. It actually has to be Int to run.

@tyleringebrand tyleringebrand changed the title Misleading error message Error message does not indicate conflicting type parameters Sep 28, 2021
@vtjnash
Copy link
Member

vtjnash commented Sep 29, 2021

julia/src/jltypes.c

Lines 1223 to 1226 in 7544b18

if (!within_typevar(params[i], bounds[2*i], bounds[2*i+1])) {
// TODO: pass a new version of `tv` containing the instantiated bounds
jl_type_error_rt(jl_symbol_name(tn->name), jl_symbol_name(tv->name), (jl_value_t*)tv, params[i]);
}

@vtjnash vtjnash added the error messages Better, more actionable error messages label Sep 29, 2021
vtjnash added a commit that referenced this issue Sep 29, 2021
vtjnash added a commit that referenced this issue Sep 30, 2021
KristofferC pushed a commit that referenced this issue Oct 19, 2021
KristofferC pushed a commit that referenced this issue Nov 11, 2021
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue Mar 8, 2022
staticfloat pushed a commit that referenced this issue Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable error messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants