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

Incorrect line with "non-boolean (Int64) used in boolean context" error #7372

Closed
nalimilan opened this issue Jun 22, 2014 · 0 comments
Closed
Assignees
Labels
kind:bug Indicates an unexpected problem or unintended behavior

Comments

@nalimilan
Copy link
Member

With the function below I made a mistake and got a very explicit error. Only the line was clearly incorrect: the problem is actually subset[i] || continue at line 38.

function test(x::AbstractVector...;
                    subset::Union(Nothing, AbstractVector{Int}, AbstractVector{Bool}, BitVector) = nothing,
                    weights::Union(Nothing, AbstractVector{Number}) = nothing)
    n = length(x)
    l = map(length, x)
    vtypes = map(eltype, x)

    for i in 1:n
        if l[1] != l[i]
            error("arguments are not of the same length: $l")
        end
    end

    if (isa(subset, AbstractVector{Bool}) || isa(subset, BitVector)) && length(subset) != l[1]
        error("'subset' (length $(length(subset))) must be of the same length as vectors (length $(l[1])) when it is boolean")
    end

    if weights != nothing && length(weights) != l[1]
        error("'weights' (length $(length(weights))) must be of the same length as vectors (length $(l[1]))")
    end

    d = cell(n)

    if weights == nothing
        counts = Array(Int, 0)
    else
        counts = Array(eltype(weights), 0)
    end

    a = Array(Int, (0, 0))
    el = cell(n)

    if subset == nothing
        subset = 1:l[1]
    end

    for i in subset
        subset[i] || continue

        for j in 1:n
            pos = findfirst(a, x[j][i])
            if pos == 0
                d = push!(d, x[j][i])
                a = cat(j, a, zeros(Int, ntuple(n - 1, k -> k >= j ? k : k + 1)))
                pos = length(d)
            end

            @inbounds el[j] = pos
        end

        @inbounds a[el...] += 1
    end
end

julia> test([1:10]);
ERROR: type: non-boolean (Int64) used in boolean context
 in test at none:9

I've tried making a shorter example, but the bug vanishes when removing apparently unrelated code. For example, this version prints the correct line number:

function test2(x::AbstractVector...;
                    subset::Union(Nothing, AbstractVector{Int}, AbstractVector{Bool}, BitVector) = nothing,
                    weights::Union(Nothing, AbstractVector{Number}) = nothing)
    n = length(x)
    l = map(length, x)
    vtypes = map(eltype, x)

    d = cell(n)

    if weights == nothing
        counts = Array(Int, 0)
    else
        counts = Array(eltype(weights), 0)
    end

    a = Array(Int, (0, 0))
    el = cell(n)

    if subset == nothing
        subset = 1:l[1]
    end

    for i in subset
        subset[i] || continue

        for j in 1:n
            pos = findfirst(a, x[j][i])
            if pos == 0
                d = push!(d, x[j][i])
                a = cat(j, a, zeros(Int, ntuple(n - 1, k -> k >= j ? k : k + 1)))
                pos = length(d)
            end

            @inbounds el[j] = pos
        end

        @inbounds a[el...] += 1
    end
end

julia> test2(x)
ERROR: type: non-boolean (Int64) used in boolean context
 in test2 at none:23

For the record, is it useful to make such reports about line numbers, or is there a well-known issue about that?

EDIT: I did not paste the second function correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants