Skip to content

SE-0067 - Failable initializers for Numeric Types #2742

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

Closed
wants to merge 9 commits into from

Conversation

ultramiraculous
Copy link
Contributor

What's in this pull request?

Adding failable initializers for builtin numeric types. Added init? methods to each numeric type and moved the Float->Int conversions into the FixedPoint template.

Resolved bug number: (SR-1491)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@ultramiraculous
Copy link
Contributor Author

Is there a test suite around this stuff that I'm missing?

@gribozavr gribozavr self-assigned this May 27, 2016
init<Source: Integer>(_ value: Source)

/// Fails if the argument cannot be exactly represented.
init?<Source: Integer>(exactly value: Source)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please restore this comment. The initializers should be generic.

@gribozavr
Copy link
Contributor

Thank you! The tests should go into validation-test/stdlib/FixedPoint.swift.gyb, validation-test/stdlib/FixedPointArithmeticTraps.swift.gyb, test/1_stdlib/FloatingPoint.swift.gyb. Feel free to create a new file in the validation test suite for floating point if necessary.

@gribozavr
Copy link
Contributor

@swift-ci Please test

@gribozavr
Copy link
Contributor

@swift-ci Please test

@gribozavr
Copy link
Contributor

@ultramiraculous Could you take a look at the build failures? Do you think they are related?

@ultramiraculous
Copy link
Contributor Author

They don't appear to be related to my changes, but I've said that before and learned my lesson. It looks like there's unused results in Foundation? (Was SE-0047 completed at some point?)

@gribozavr
Copy link
Contributor

gribozavr commented May 28, 2016

Quite possibly so, but unused results should only be warnings. In this case, we're failing the swiftpm bootstrap on Linux.

SE-0047 has a complete implementation now.

@ultramiraculous
Copy link
Contributor Author

Just got around to taking another look. It looks like the OS X test failed for a different reason than the Linux one:

/Users/buildnode/jenkins/workspace/swift-PR-osx/swift/stdlib/public/core/FloatingPointTypes.swift.gyb:740:18: error: integer literal '4503599627370495' overflows when stored into 'UInt'
    if (value) > 4503599627370495 && UInt(self) != value {

Which is clearly my fault. I'll look at a fix.

@ultramiraculous
Copy link
Contributor Author

I switched FloatingPointTypes to use all_integer_types like FixedPoint. I didn't notice 'Int' was in FloatingPointTypes's allIntBits, and "Int" > $a_number is always true, which screwed up my logic.

I have tests mostly done, but is there a good way to build + test just the stdlib? I'm relying on hacked up build scripts to try to validate my work in a reasonable timeframe, which I imagine isn't the right way to do this?

@gribozavr
Copy link
Contributor

Thank you! The usual way to test is to run build-script -RT.

@gribozavr
Copy link
Contributor

We can also run the tests on CI.

@swift-ci Please test

@ultramiraculous
Copy link
Contributor Author

Added some tests to try to hit some edge cases.

@ultramiraculous
Copy link
Contributor Author

Still not sure what to make of the build failures, any insight @gribozavr?

@moiseev
Copy link
Contributor

moiseev commented Jun 7, 2016

Since the time it was last looked at, there are some merge conflicts. @ultramiraculous could you please resolve them?

@ultramiraculous ultramiraculous force-pushed the failure branch 3 times, most recently from 51f5c3c to c1fbda1 Compare June 7, 2016 04:23
@moiseev
Copy link
Contributor

moiseev commented Jun 7, 2016

@swift-ci Please test


from SwiftIntTypes import all_integer_types

word_bits = 4
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look right.

@ultramiraculous ultramiraculous force-pushed the failure branch 2 times, most recently from 5543333 to 5f4b8f7 Compare June 8, 2016 00:46
@ultramiraculous
Copy link
Contributor Author

@moiseev / @gribozavr Would it make sense to just break this up into 2 or 4 PRs? It got a bit larger than I'd usually prefer to have reviewed after adding the tests, and maybe we could at least get the stable parts of it merged in.

@gribozavr
Copy link
Contributor

@ultramiraculous Absolutely, splitting the PR would make things easier! I just have one small request -- while splitting, could you fold the tests for each incremental step into PRs that add or change the APIs?

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.

3 participants