Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ function showerror(io::IO, ex::MethodError)
print(io, "\nFor element-wise $nounf, use broadcasting with dot syntax: $first .$fstring $second")
end
end
# catch transpose and adjoint on arrays with unsupported element types
if f isa Function && (f === transpose || f === adjoint) && length(san_arg_types_param) == 1
# if the argument is not an array, it's likely an element type that doesn't support the operation
if !(san_arg_types_param[1] <: AbstractArray)
fname = f === transpose ? "transpose" : "adjoint"
print(io, "\n$fname is a linear-algebra operation that applies recursively to elements.")
print(io, "\nTo permute dimensions of an array, use permutedims.")
end
end
if ft <: AbstractArray
print(io, "\nIn case you're trying to index into the array, use square brackets [] instead of parentheses ().")
end
Expand Down
17 changes: 17 additions & 0 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,23 @@ let err_str
@test occursin("For element-wise subtraction, use broadcasting with dot syntax: array .- scalar", err_str)
end

# Issue 39938 - transpose/adjoint hint for unsupported element types
let err_str
struct NoTransposeType x::Int end
v = [NoTransposeType(1)]
err_str = @except_str transpose(v)[1,1] MethodError
@test occursin("MethodError: no method matching transpose(", err_str)
@test occursin("transpose is a linear-algebra operation that applies recursively to elements.", err_str)
@test occursin("To permute dimensions of an array, use permutedims.", err_str)

struct NoAdjointType x::Int end
v2 = [NoAdjointType(1)]
err_str = @except_str adjoint(v2)[1,1] MethodError
@test occursin("MethodError: no method matching adjoint(", err_str)
@test occursin("adjoint is a linear-algebra operation that applies recursively to elements.", err_str)
@test occursin("To permute dimensions of an array, use permutedims.", err_str)
end

import Core: String
method_defs_lineno = @__LINE__() + 1
String() = throw(ErrorException("1"))
Expand Down