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

[time.cal.year.nonmembers] Avoid narrowing conversion #4184

Merged
merged 1 commit into from Dec 14, 2020

Conversation

jwakely
Copy link
Member

@jwakely jwakely commented Sep 10, 2020

The years::rep type could be a signed integer wider than int.

@@ -4771,7 +4771,7 @@
\begin{itemdescr}
\pnum
\returns
\tcode{year\{int\{x\} + y.count()\}}.
\tcode{year\{int\{x\} + static_cast<int>(y.count())\}}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a precondition that y.count() is representable by int to avoid UB in the conversion. It might be simplest to switch from \returns: meow to \effects: Equivalent to: \tcode{return meow;} so the precondition is implicit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion is never UB. The addition might, but it could be today as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I forget that everything's twos-complement now. I should probably file an LWG issue for the wider problem of "Returns: non-well-defined"; I'm reasonably certain we want implied preconditions, but we don't say so.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for grins, the recommendation is to stuff this result into a signed 16 bit integral (short). :-) Rationale: If your civil year number is outside of the range +/-32K, you're hosed anyway. The civil calendar models the solar system with an accuracy of about 1 day in 4K years.

The years::rep type could be a signed integer wider than int.
@zygoloid zygoloid added the lwg Issue must be reviewed by LWG. label Sep 18, 2020
@zygoloid
Copy link
Member

Adding LWG label: while the narrowing check here may be unintentional, it seems to be a normative requirement (whatever it means for the "Returns:" clause to be ill-formed), so this PR seems superficially to be a normative change. I'd like LWG to consider this; if the conclusion is that "Returns:" should never be ill-formed and so it's just an obvious wording error that it might be in this case, then I think we can treat this as editorial.

@jensmaurer
Copy link
Member

@jwakely, are you dealing with the LWG issue here?

@jwakely
Copy link
Member Author

jwakely commented Sep 18, 2020

I already raised it and LWG was OK with fixing the unintended flaw editorially.

https://lists.isocpp.org/lib/2020/09/17424.php

I should have provided that info in the PR, sorry.

@jwakely
Copy link
Member Author

jwakely commented Oct 28, 2020

Can we remove the lwg label? As I said above, LWG have already discussed it an agreed that resolving it editorially would be best.

@jensmaurer jensmaurer removed the lwg Issue must be reviewed by LWG. label Oct 28, 2020
@tkoeppe tkoeppe merged commit 29022b4 into cplusplus:master Dec 14, 2020
@jwakely jwakely deleted the cal-narrowing branch February 25, 2021 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants