diff --git a/base/errorshow.jl b/base/errorshow.jl index 69b67bf36953a..fb15f09fbfe2c 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -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 diff --git a/test/errorshow.jl b/test/errorshow.jl index 941c5e2074f51..45722f2f96cf5 100644 --- a/test/errorshow.jl +++ b/test/errorshow.jl @@ -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"))