Skip to content

Commit 8ab062d

Browse files
authored
Avoid non-lazy string interpolation and replace @assert with ArgumentError
1 parent 463e830 commit 8ab062d

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

src/config.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct InvalidTagException{E,O} <: Exception
2929
end
3030

3131
Base.showerror(io::IO, e::InvalidTagException{E,O}) where {E,O} =
32-
print(io, "Invalid Tag object:\n Expected $E,\n Observed $O.")
32+
print(io, "Invalid Tag object:\n Expected ", E, ",\n Observed ", O, ".")
3333

3434
checktag(::Type{Tag{FT,VT}}, f::F, x::AbstractArray{V}) where {FT,VT,F,V} =
3535
throw(InvalidTagException{Tag{F,V},Tag{FT,VT}}())

src/dual.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ struct DualMismatchError{A,B} <: Exception
3535
end
3636

3737
Base.showerror(io::IO, e::DualMismatchError{A,B}) where {A,B} =
38-
print(io, "Cannot determine ordering of Dual tags $(e.a) and $(e.b)")
38+
print(io, "Cannot determine ordering of Dual tags ", e.a, " and ", e.b)
3939

4040
@noinline function throw_cannot_dual(V::Type)
41-
throw(ArgumentError("Cannot create a dual over scalar type $V." *
42-
" If the type behaves as a scalar, define ForwardDiff.can_dual(::Type{$V}) = true."))
41+
throw(ArgumentError(lazy"Cannot create a dual over scalar type $V. If the type behaves as a scalar, define ForwardDiff.can_dual(::Type{$V}) = true."))
4342
end
4443

4544
"""

src/gradient.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ end
113113

114114
function chunk_mode_gradient_expr(result_definition::Expr)
115115
return quote
116-
@assert structural_length(x) >= N "chunk size cannot be greater than ForwardDiff.structural_length(x) ($(N) > $(structural_length(x)))"
116+
if structural_length(x) < N
117+
throw(ArgumentError(lazy"chunk size cannot be greater than ForwardDiff.structural_length(x) ($(N) > $(structural_length(x)))"))
118+
end
117119

118120
# precalculate loop bounds
119121
xlen = structural_length(x)

src/jacobian.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ const JACOBIAN_ERROR = DimensionMismatch("jacobian(f, x) expects that f(x) is an
169169
function jacobian_chunk_mode_expr(work_array_definition::Expr, compute_ydual::Expr,
170170
result_definition::Expr, y_definition::Expr)
171171
return quote
172-
@assert structural_length(x) >= N "chunk size cannot be greater than ForwardDiff.structural_length(x) ($(N) > $(structural_length(x)))"
172+
if structural_length(x) < N
173+
throw(ArgumentError(lazy"chunk size cannot be greater than ForwardDiff.structural_length(x) ($(N) > $(structural_length(x)))"))
174+
end
173175

174176
# precalculate loop bounds
175177
xlen = structural_length(x)

test/DualTest.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ ForwardDiff.:≺(::Type{OuterTestTag}, ::Type{TestTag}) = false
507507
@eval begin
508508
x = rand() + $modifier
509509
dx = @inferred $M.$f(Dual{TestTag}(x, one(x)))
510-
actualval = $M.$f(x)
511-
@assert actualval isa Real || actualval isa Complex
510+
actualval = $M.$f(x)::Union{Real,Complex}
512511
if actualval isa Real
513512
@test dx isa Dual{TestTag}
514513
@test value(dx) == actualval
@@ -536,8 +535,7 @@ ForwardDiff.:≺(::Type{OuterTestTag}, ::Type{TestTag}) = false
536535
dy = @inferred $M.$f(x, Dual{TestTag}(y, one(y)))
537536
actualdx = $(derivs[1])
538537
actualdy = $(derivs[2])
539-
actualval = $M.$f(x, y)
540-
@assert actualval isa Real || actualval isa Complex
538+
actualval = $M.$f(x, y)::Union{Real,Complex}
541539
if actualval isa Real
542540
@test dx isa Dual{TestTag}
543541
@test dy isa Dual{TestTag}

0 commit comments

Comments
 (0)