-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
irrationals: restrict assume effects annotations to known types #55886
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,26 @@ Base.@irrational γ euler | |||||||||||||||||||||||
Base.@irrational φ (1+sqrt(big(5)))/2 | ||||||||||||||||||||||||
Base.@irrational catalan catalan | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
const _KnownIrrational = Union{ | ||||||||||||||||||||||||
typeof(π), typeof(ℯ), typeof(γ), typeof(φ), typeof(catalan) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
function Rational{BigInt}(::_KnownIrrational) | ||||||||||||||||||||||||
Base._throw_argument_error_irrational_to_rational_bigint() | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
Base.@assume_effects :foldable function Rational{T}(x::_KnownIrrational) where {T<:Integer} | ||||||||||||||||||||||||
Base._irrational_to_rational(T, x) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
Base.@assume_effects :foldable function (::Type{T})(x::_KnownIrrational, r::RoundingMode) where {T<:Union{Float32,Float64}} | ||||||||||||||||||||||||
Base._irrational_to_float(T, x, r) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
Base.@assume_effects :foldable function rationalize(::Type{T}, x::_KnownIrrational; tol::Real=0) where {T<:Integer} | ||||||||||||||||||||||||
Base._rationalize_irrational(T, x, tol) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
Base.@assume_effects :foldable function Base.lessrational(rx::Rational, x::_KnownIrrational) | ||||||||||||||||||||||||
Base._lessrational(rx, x) | ||||||||||||||||||||||||
end | ||||||||||||||||||||||||
Comment on lines
+19
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be added to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Perhaps this was the intent, but there's nothing stopping users from doing this. Wrong effects may have catastrophic consequences like crashes or silently wrong results, or other hard-to-debug issues (undefined behavior, in short), so they're extremely undesirable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. My only point was that maybe my suggestion is not worth it since it is supposed to be only relevant for uses in Base anyway. But even then it might be nice not to have to remember to update the union if a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This intent is documented (but of course that doesn't prevent definitions outside of Base): │ Warning
│
│ This macro should not be used outside of Base Julia.
│
│ The macro creates a new type Irrational{:sym} regardless of where
│ it's invoked. This can lead to conflicting definitions if two
│ packages define an irrational number with the same name but
│ different values. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The macro is irrelevant to the question of safety here. As shown in the fixed issue, it's not required to add new subtypes of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think there's no other way to achieve safety here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I think there's a misunderstanding here, I'll try to clarify my initial comment:
This wouldn't reduce the amount of code here but would ensure that for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you mean to add the methods with effect annotations using the macro instead of like in this PR. Yeah, I guess that would be correct, but it'd also unfortunately increase the number of methods. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering whether the increased number of methods would be problematic. My gut feeling was that it might be fine given that it's only five |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
# aliases | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
π | ||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
Base.rationalize
. Right now it creates a new functionBase.MathConstants.rationalize
. Consequently,Base.rationalize(Int, π)
isn’t constant-folded in Julia 1.10 and 1.11:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment easily gets lost so I think it is preferable to create a new issue where it can be given a milestone for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened a PR: #56793