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

const fn for SocketAddr::new, SocketAddrV4::new and SocketAddrV6::new #67315

Closed
wants to merge 2 commits into from

Conversation

gliderkite
Copy link

Add const fn to SocketAddr::new, SocketAddrV4::new and SocketAddrV6::new to be able to initialize the socket address at compile time, for example:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6, SocketAddr};

const localhost_v4: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const socket_v4: SocketAddrV4 = SocketAddrV4::new(localhost_v4, 80);
const socket_add_v4: SocketAddr = SocketAddr::new(IpAddr::V4(localhost_v4), 80);

const localhost_v6: Ipv6Addr = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
const socket_v6: SocketAddrV6 = SocketAddrV6::new(localhost_v6, 80, 0, 0);
const socket_add_v6: SocketAddr = SocketAddr::new(IpAddr::V6(localhost_v6), 80);

This addition is probably still missing the right method attributes:

rustc_const_stable(feature = "const_X", since = "1.?"))

as I'm not aware of the procedure required to add const to an existing method in stable.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 15, 2019
@rust-highfive
Copy link
Collaborator

The job mingw-check of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-12-15T11:44:34.5115012Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-12-15T11:44:35.4655442Z ##[command]git config gc.auto 0
2019-12-15T11:44:35.4662368Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-12-15T11:44:35.4669456Z ##[command]git config --get-all http.proxy
2019-12-15T11:44:35.4677270Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/67315/merge:refs/remotes/pull/67315/merge
---
2019-12-15T11:49:36.8972483Z     Checking panic_unwind v0.0.0 (/checkout/src/libpanic_unwind)
2019-12-15T11:49:41.2543519Z error[E0723]: loops and conditional expressions are not stable in const fn
2019-12-15T11:49:41.2544847Z    --> src/libstd/net/addr.rs:130:13
2019-12-15T11:49:41.2545322Z     |
2019-12-15T11:49:41.2545909Z 130 |             IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
2019-12-15T11:49:41.2546843Z     |
2019-12-15T11:49:41.2546843Z     |
2019-12-15T11:49:41.2547445Z     = note: for more information, see issue ***/issues/57563
2019-12-15T11:49:41.2548171Z     = help: add `#![feature(const_fn)]` to the crate attributes to enable
2019-12-15T11:49:41.2548577Z 
2019-12-15T11:49:41.2563242Z error[E0723]: can only call other `const fn` within a `const fn`, but `const net::hton::<u16>` is not stable as `const fn`
2019-12-15T11:49:41.2564537Z     |
2019-12-15T11:49:41.2564537Z     |
2019-12-15T11:49:41.2565089Z 279 |                 sin_port: hton(port),
2019-12-15T11:49:41.2566107Z     |
2019-12-15T11:49:41.2566107Z     |
2019-12-15T11:49:41.2566885Z     = note: for more information, see issue ***/issues/57563
2019-12-15T11:49:41.2567434Z     = help: add `#![feature(const_fn)]` to the crate attributes to enable
2019-12-15T11:49:41.2571144Z 
2019-12-15T11:49:41.2578426Z error[E0723]: can only call other `const fn` within a `const fn`, but `const net::hton::<u16>` is not stable as `const fn`
2019-12-15T11:49:41.2581376Z     |
2019-12-15T11:49:41.2581376Z     |
2019-12-15T11:49:41.2583718Z 371 |                 sin6_port: hton(port),
2019-12-15T11:49:41.2585997Z     |
2019-12-15T11:49:41.2585997Z     |
2019-12-15T11:49:41.2587354Z     = note: for more information, see issue ***/issues/57563
2019-12-15T11:49:41.2588686Z     = help: add `#![feature(const_fn)]` to the crate attributes to enable
2019-12-15T11:49:42.0212674Z error: aborting due to 3 previous errors
2019-12-15T11:49:42.0212761Z 
2019-12-15T11:49:42.0213032Z For more information about this error, try `rustc --explain E0723`.
2019-12-15T11:49:42.0670601Z error: could not compile `std`.
---
2019-12-15T11:49:42.0796053Z   local time: Sun Dec 15 11:49:42 UTC 2019
2019-12-15T11:49:42.3561560Z   network time: Sun, 15 Dec 2019 11:49:42 GMT
2019-12-15T11:49:42.3561743Z == end clock drift check ==
2019-12-15T11:49:49.2429295Z 
2019-12-15T11:49:49.2536789Z ##[error]Bash exited with code '1'.
2019-12-15T11:49:49.2560521Z ##[section]Starting: Checkout
2019-12-15T11:49:49.2561989Z ==============================================================================
2019-12-15T11:49:49.2562194Z Task         : Get sources
2019-12-15T11:49:49.2562249Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@tesuji tesuji mentioned this pull request Dec 15, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Dec 16, 2019

These functions can't be stable yet, as they use match, and match isn't stable in const fn yet. You can mark the functions as unstably const with #[rustc_const_unstable(feature = "const_socket_new", issue="1234")] and open a tracking issue (and use that number instead of 1234). This way it is at least available on nightly as const fn

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Dec 17, 2019
make htons const fn

This may partially help rust-lang#67315.
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Dec 18, 2019
make htons const fn

This may partially help rust-lang#67315.
tmandry added a commit to tmandry/rust that referenced this pull request Dec 18, 2019
make htons const fn

This may partially help rust-lang#67315.
@rust-highfive
Copy link
Collaborator

The job mingw-check of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-12-18T08:59:14.4253070Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-12-18T08:59:14.4451551Z ##[command]git config gc.auto 0
2019-12-18T08:59:14.4515810Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-12-18T08:59:14.4561891Z ##[command]git config --get-all http.proxy
2019-12-18T08:59:14.4705627Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/67315/merge:refs/remotes/pull/67315/merge
---
2019-12-18T09:04:25.5671258Z     Checking panic_abort v0.0.0 (/checkout/src/libpanic_abort)
2019-12-18T09:04:25.7394499Z     Checking backtrace v0.3.40
2019-12-18T09:04:27.2508864Z     Checking rustc-std-workspace-alloc v1.99.0 (/checkout/src/tools/rustc-std-workspace-alloc)
2019-12-18T09:04:27.2868166Z     Checking panic_unwind v0.0.0 (/checkout/src/libpanic_unwind)
2019-12-18T09:04:29.1767733Z error[E0541]: unknown meta item 'issue'
2019-12-18T09:04:29.1768418Z     |
2019-12-18T09:04:29.1768418Z     |
2019-12-18T09:04:29.1768693Z 128 |     #[rustc_const_unstable(feature = "const_socket_new", issue = "67390")]
2019-12-18T09:04:29.1769043Z     |                                                          ^^^^^^^^^^^^^^^ expected one of `feature`
2019-12-18T09:04:29.1769087Z 
2019-12-18T09:04:29.1769318Z error[E0541]: unknown meta item 'issue'
2019-12-18T09:04:29.1769720Z     |
2019-12-18T09:04:29.1769720Z     |
2019-12-18T09:04:29.1770355Z 276 |     #[rustc_const_unstable(feature = "const_socket_new", issue = "67390")]
2019-12-18T09:04:29.1770819Z     |                                                          ^^^^^^^^^^^^^^^ expected one of `feature`
2019-12-18T09:04:29.1770920Z 
2019-12-18T09:04:29.1776870Z error[E0541]: unknown meta item 'issue'
2019-12-18T09:04:29.1777657Z     |
2019-12-18T09:04:29.1777657Z     |
2019-12-18T09:04:29.1778065Z 369 |     #[rustc_const_unstable(feature = "const_socket_new", issue = "67390")]
2019-12-18T09:04:29.1778526Z     |                                                          ^^^^^^^^^^^^^^^ expected one of `feature`
2019-12-18T09:04:30.8522552Z error: aborting due to 3 previous errors
2019-12-18T09:04:30.8523081Z 
2019-12-18T09:04:30.8523569Z For more information about this error, try `rustc --explain E0541`.
2019-12-18T09:04:30.8883841Z error: could not compile `std`.
---
2019-12-18T09:04:30.8982242Z   local time: Wed Dec 18 09:04:30 UTC 2019
2019-12-18T09:04:31.1753465Z   network time: Wed, 18 Dec 2019 09:04:31 GMT
2019-12-18T09:04:31.1754149Z == end clock drift check ==
2019-12-18T09:04:38.9761385Z 
2019-12-18T09:04:38.9855744Z ##[error]Bash exited with code '1'.
2019-12-18T09:04:38.9879861Z ##[section]Starting: Checkout
2019-12-18T09:04:38.9881653Z ==============================================================================
2019-12-18T09:04:38.9881697Z Task         : Get sources
2019-12-18T09:04:38.9881733Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Dec 18, 2019
make htons const fn

This may partially help rust-lang#67315.
@shepmaster
Copy link
Member

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned shepmaster Dec 18, 2019
@shepmaster
Copy link
Member

It's worth it to trust but verify what anyone suggests as a fix. In this case, it appears that the attribute should be #[rustc_const_unstable(feature = "...")]; no issue.

@oli-obk
Copy link
Contributor

oli-obk commented Dec 18, 2019

Schrödinger bootstrap strikes again. We both need the issue field and we don't need it. You need to use cfg_attr(bootstrap, ... to get this working, OR you wait two days until the next beta is cut ;) Then a lot of bootstrap nonsense finally disappears.

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Dec 19, 2019
make htons const fn

This may partially help rust-lang#67315.
Centril added a commit to Centril/rust that referenced this pull request Dec 19, 2019
make htons const fn

This may partially help rust-lang#67315.
@JohnTitor JohnTitor added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 12, 2020
@JohnCSimon
Copy link
Member

JohnCSimon commented Jan 18, 2020

Ping from triage - @gliderkite - can you address the build issues in this PR? Thank you.

@gliderkite
Copy link
Author

@JohnCSimon sorry for the late reply, could you please tell me how can I build and test the changes to make sure it won't fail on CI (without relying on CI to do that if possible)? Is there a doc/reference I can have a look at that states how do do it, before I apply the fix? Thanks.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 23, 2020

It should suffice to run ./x.py test --stage 1 src/test/ui --bless for your case

@oli-obk
Copy link
Contributor

oli-obk commented Jan 23, 2020

I think it would also be good to add the example from your main post as a test file in src/tests/ui/consts

@gliderkite
Copy link
Author

I might have missed something, or proceed in the wrong way, please forgive my inexperience with the subject.

When I try to compile with ./x.py test --stage 1 src/test/ui --bless as suggested I get the (expected) error:

error[E0658]: `match` is not allowed in a `const fn`
   --> src/libstd/net/addr.rs:130:9
    |
130 | /         match ip {
131 | |             IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
132 | |             IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
133 | |         }
    | |_________^
    |
    = note: for more information, see https://github.com/rust-lang/rust/issues/49146
    = help: add `#![feature(const_if_match)]` to the crate attributes to enable

Adding #![feature(const_if_match)] to src/libstd/lib.rs and I get:

error: `net::addr::SocketAddrV4::new` is not yet stable as a const fn
   --> src/libstd/net/addr.rs:131:45
    |
131 |             IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
    |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_socket_new)]` to the crate attributes to enable

error: `net::addr::SocketAddrV6::new` is not yet stable as a const fn
   --> src/libstd/net/addr.rs:132:45
    |
132 |             IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
    |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_socket_new)]` to the crate attributes to enable

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libstd/net/addr.rs:276:28
    |
276 |                 sin_addr: *ip.as_inner(),
    |                            ^^^^^^^^^^^^^

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libstd/net/addr.rs:277:28
    |
277 |                 ..unsafe { mem::zeroed() }
    |                            ^^^^^^^^^^^^^

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libstd/net/addr.rs:369:29
    |
369 |                 sin6_addr: *ip.as_inner(),
    |                             ^^^^^^^^^^^^^

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libstd/net/addr.rs:372:28
    |
372 |                 ..unsafe { mem::zeroed() }
    |                            ^^^^^^^^^^^^^

error: aborting due to 6 previous errors

Replacing mem::zeroed() with mem::MaybeUninit::zeroed().assume_init() and adding (in src/libstd/net/ip.rs) const to:

impl AsInner<c::in6_addr> for Ipv6Addr {
    const fn as_inner(&self) -> &c::in6_addr {
        &self.inner
    }
}

leads to the following errors:

error[E0658]: references in constant functions may only refer to immutable values
   --> src/libcore/mem/maybe_uninit.rs:360:13
    |
360 |             u.as_mut_ptr().write_bytes(0u8, 1);
    |             ^ constant functions require immutable values
    |
    = note: for more information, see https://github.com/rust-lang/rust/issues/57349
    = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libcore/mem/maybe_uninit.rs:360:13
    |
360 |             u.as_mut_ptr().write_bytes(0u8, 1);
    |             ^^^^^^^^^^^^^^

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> src/libcore/mem/maybe_uninit.rs:360:13
    |
360 |             u.as_mut_ptr().write_bytes(0u8, 1);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

I guess that this is therefore blocked at least on #57349.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 24, 2020

Mutable references could be added by adding the const_mut_refs feature gate, but the killer is

impl AsInner<c::in6_addr> for Ipv6Addr {
    const fn as_inner(&self) -> &c::in6_addr {
        &self.inner
    }
}

This isn't possible right now. You can't make trait methods const fn.

I'm not sure why the as_inner function is a trait function, but if it needs to stay a trait function, then you're indeed out of luck and this can't become const fn right now. The feature is actively being developed, so there's a good chance it'll be possible in the next few months.

@JohnCSimon JohnCSimon added S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Feb 2, 2020
@JohnCSimon
Copy link
Member

Ping from triage: @gliderkite - if you think it's appropriate, please close this PR out as S-blocked-closed

@gliderkite gliderkite closed this Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants