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

ptr: introduce len() method on raw slices #71082

Merged
merged 5 commits into from
Apr 15, 2020

Conversation

neocturne
Copy link
Contributor

@neocturne neocturne commented Apr 13, 2020

It is already possible to extract the pointer part of a raw slice by a
simple cast, but retrieving the length is not possible without relying
on the representation of the raw slice when it is not valid to convert
the raw slice into a slice reference (i.e. the pointer is null or
unaligned).

Introduce a new function ptr::slice_len() to add this missing feature.

Introduce a len() method on raw slices to add this missing feature.

@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 Apr 13, 2020
@Centril
Copy link
Contributor

Centril commented Apr 13, 2020

cc @rust-lang/wg-const-eval
cc @rust-lang/libs
r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned shepmaster Apr 13, 2020
@Mark-Simulacrum
Copy link
Member

One question I have is whether we can put this onto slices directly, such that [T]::len is actually implemented with the signature of this method. I'm not sure that would actually in practice be a breaking change (if you have a reference, you definitely have *const [T])...

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-7 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.
2020-04-13T00:43:55.9087814Z ========================== Starting Command Output ===========================
2020-04-13T00:43:55.9090107Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/12fcb78a-edba-4576-a399-94178ef00a61.sh
2020-04-13T00:43:55.9090389Z 
2020-04-13T00:43:55.9094309Z ##[section]Finishing: Disable git automatic line ending conversion
2020-04-13T00:43:55.9112927Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/71082/merge to s
2020-04-13T00:43:55.9117514Z Task         : Get sources
2020-04-13T00:43:55.9117789Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-13T00:43:55.9118008Z Version      : 1.0.0
2020-04-13T00:43:55.9118154Z Author       : Microsoft
---
2020-04-13T00:43:57.1681119Z ##[command]git remote add origin https://github.com/rust-lang/rust
2020-04-13T00:43:57.1689147Z ##[command]git config gc.auto 0
2020-04-13T00:43:57.1695177Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2020-04-13T00:43:57.1700590Z ##[command]git config --get-all http.proxy
2020-04-13T00:43:57.1710023Z ##[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/71082/merge:refs/remotes/pull/71082/merge
---
2020-04-13T00:46:11.6285182Z Looks like docker image is the same as before, not uploading
2020-04-13T00:46:19.2671202Z [CI_JOB_NAME=x86_64-gnu-llvm-7]
2020-04-13T00:46:19.2939390Z [CI_JOB_NAME=x86_64-gnu-llvm-7]
2020-04-13T00:46:19.2960274Z == clock drift check ==
2020-04-13T00:46:19.2968723Z   local time: Mon Apr 13 00:46:19 UTC 2020
2020-04-13T00:46:19.3486840Z   network time: Mon, 13 Apr 2020 00:46:19 GMT
2020-04-13T00:46:19.3579386Z Starting sccache server...
2020-04-13T00:46:19.4211472Z configure: processing command line
2020-04-13T00:46:19.4212086Z configure: 
2020-04-13T00:46:19.4213362Z configure: rust.dist-src        := False
---
2020-04-13T00:51:26.1844565Z    Compiling rustc_feature v0.0.0 (/checkout/src/librustc_feature)
2020-04-13T00:51:27.6277044Z    Compiling fmt_macros v0.0.0 (/checkout/src/libfmt_macros)
2020-04-13T00:51:29.1282939Z    Compiling rustc_ast_pretty v0.0.0 (/checkout/src/librustc_ast_pretty)
2020-04-13T00:51:31.0648183Z    Compiling rustc_hir v0.0.0 (/checkout/src/librustc_hir)
2020-04-13T00:51:38.9787691Z    Compiling rustc_query_system v0.0.0 (/checkout/src/librustc_query_system)
2020-04-13T00:51:42.3325941Z    Compiling rustc_hir_pretty v0.0.0 (/checkout/src/librustc_hir_pretty)
2020-04-13T00:51:46.7215664Z    Compiling rustc_attr v0.0.0 (/checkout/src/librustc_attr)
2020-04-13T00:51:50.8240749Z    Compiling rustc_parse v0.0.0 (/checkout/src/librustc_parse)
2020-04-13T00:51:59.0170576Z    Compiling rustc_ast_lowering v0.0.0 (/checkout/src/librustc_ast_lowering)
---
2020-04-13T01:13:49.2941657Z    Compiling rustc_feature v0.0.0 (/checkout/src/librustc_feature)
2020-04-13T01:13:50.9916038Z    Compiling fmt_macros v0.0.0 (/checkout/src/libfmt_macros)
2020-04-13T01:13:52.9500995Z    Compiling rustc_ast_pretty v0.0.0 (/checkout/src/librustc_ast_pretty)
2020-04-13T01:13:55.4127254Z    Compiling rustc_hir v0.0.0 (/checkout/src/librustc_hir)
2020-04-13T01:14:05.0558005Z    Compiling rustc_query_system v0.0.0 (/checkout/src/librustc_query_system)
2020-04-13T01:14:08.9416992Z    Compiling rustc_hir_pretty v0.0.0 (/checkout/src/librustc_hir_pretty)
2020-04-13T01:14:14.2412403Z    Compiling rustc_attr v0.0.0 (/checkout/src/librustc_attr)
2020-04-13T01:14:19.4912904Z    Compiling rustc_parse v0.0.0 (/checkout/src/librustc_parse)
2020-04-13T01:14:29.2672906Z    Compiling rustc_ast_lowering v0.0.0 (/checkout/src/librustc_ast_lowering)
---
2020-04-13T01:38:58.9899573Z .................................................................................................... 1700/9889
2020-04-13T01:39:02.9907497Z .................................................................................................... 1800/9889
2020-04-13T01:39:11.1173862Z .................................................................................................... 1900/9889
2020-04-13T01:39:19.0199220Z ....i............................................................................................... 2000/9889
2020-04-13T01:39:25.9956090Z ..............................................................................................iiiii. 2100/9889
2020-04-13T01:39:45.4926981Z .................................................................................................... 2300/9889
2020-04-13T01:39:47.5374859Z .................................................................................................... 2400/9889
2020-04-13T01:39:50.0007150Z .................................................................................................... 2500/9889
2020-04-13T01:39:55.1580827Z .................................................................................................... 2600/9889
---
2020-04-13T01:42:49.6714874Z .................................................................................................... 5100/9889
2020-04-13T01:42:57.0374866Z .................................................................................................... 5200/9889
2020-04-13T01:43:01.9194959Z ..............i..................................................................................... 5300/9889
2020-04-13T01:43:11.5849628Z .................................................................................................... 5400/9889
2020-04-13T01:43:16.5628349Z ...ii.ii........i...i............................................................................... 5500/9889
2020-04-13T01:43:23.9301762Z ................................................i................................................... 5700/9889
2020-04-13T01:43:33.8039758Z ....................................................................ii.............................. 5800/9889
2020-04-13T01:43:39.7539775Z .......i............................................................................................ 5900/9889
2020-04-13T01:43:45.1253093Z .................................................................................................... 6000/9889
2020-04-13T01:43:45.1253093Z .................................................................................................... 6000/9889
2020-04-13T01:43:54.8202462Z .................................................................................................... 6100/9889
2020-04-13T01:44:05.5686723Z .ii...i..ii...........i............................................................................. 6200/9889
2020-04-13T01:44:19.8640586Z .................................................................................................... 6400/9889
2020-04-13T01:44:26.0387341Z .................................................................................................... 6500/9889
2020-04-13T01:44:26.0387341Z .................................................................................................... 6500/9889
2020-04-13T01:44:42.4599468Z ...............................i..ii................................................................ 6600/9889
2020-04-13T01:45:03.4599254Z .................................................................................................... 6800/9889
2020-04-13T01:45:05.4197611Z ...............................i.................................................................... 6900/9889
2020-04-13T01:45:07.3246808Z .................................................................................................... 7000/9889
2020-04-13T01:45:09.3802156Z ......................................................................i............................. 7100/9889
---
2020-04-13T01:46:42.1464727Z .................................................................................................... 7800/9889
2020-04-13T01:46:45.9709969Z .................................................................................................... 7900/9889
2020-04-13T01:46:52.1123305Z .................................................................................................... 8000/9889
2020-04-13T01:46:58.3836663Z ....................................i............................................................... 8100/9889
2020-04-13T01:47:06.8681208Z ....................................................................................iiiiii.iiiii.i.. 8200/9889
2020-04-13T01:47:21.6739686Z ..............................i......i.............................................................. 8400/9889
2020-04-13T01:47:24.8034278Z .................................................................................................... 8500/9889
2020-04-13T01:47:34.6951588Z .................................................................................................... 8600/9889
2020-04-13T01:47:46.8113037Z .................................................................................................... 8700/9889
---
2020-04-13T01:50:02.2853612Z Suite("src/test/codegen") not skipped for "bootstrap::test::Codegen" -- not in ["src/tools/tidy"]
2020-04-13T01:50:02.3073622Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-13T01:50:02.4970571Z 
2020-04-13T01:50:02.4970892Z running 185 tests
2020-04-13T01:50:05.1019849Z iiii......i............ii.i..iiii....i....i...........i............i..i..................i....i..... 100/185
2020-04-13T01:50:07.5394443Z .......i.i.i...iii..iiiiiiiiiiiiiiii........................iii..............ii......
2020-04-13T01:50:07.5398200Z 
2020-04-13T01:50:07.5398381Z  finished in 5.231
2020-04-13T01:50:07.5407790Z Suite("src/test/codegen-units") not skipped for "bootstrap::test::CodegenUnits" -- not in ["src/tools/tidy"]
2020-04-13T01:50:07.5579894Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-13T01:50:09.5110662Z Suite("src/test/assembly") not skipped for "bootstrap::test::Assembly" -- not in ["src/tools/tidy"]
2020-04-13T01:50:09.5295065Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-13T01:50:09.6685950Z 
2020-04-13T01:50:09.6686221Z running 9 tests
2020-04-13T01:50:09.6687133Z iiiiiiiii
2020-04-13T01:50:09.6690293Z 
2020-04-13T01:50:09.6695832Z Suite("src/test/incremental") not skipped for "bootstrap::test::Incremental" -- not in ["src/tools/tidy"]
2020-04-13T01:50:09.6698616Z  finished in 0.139
2020-04-13T01:50:09.6872289Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-13T01:50:28.0681256Z Suite("src/test/debuginfo") not skipped for "bootstrap::test::Debuginfo" -- not in ["src/tools/tidy"]
2020-04-13T01:50:28.0897494Z Check compiletest suite=debuginfo mode=debuginfo (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2020-04-13T01:50:28.2663837Z 
2020-04-13T01:50:28.2665274Z running 115 tests
2020-04-13T01:50:40.7497791Z iiiii..i.....i..i...i..i.i.i..i..i..ii....i.i....ii..........iiii.........i.....i..i.......ii.i.ii.. 100/115
2020-04-13T01:50:42.5092831Z ...iiii.....ii.
2020-04-13T01:50:42.5099585Z 
2020-04-13T01:50:42.5099711Z  finished in 14.419
2020-04-13T01:50:42.5100438Z Suite("src/test/ui-fulldeps") not skipped for "bootstrap::test::UiFullDeps" -- not in ["src/tools/tidy"]
2020-04-13T01:50:42.5101909Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
---
2020-04-13T02:02:42.3025675Z 
2020-04-13T02:02:42.3028480Z    Doc-tests core
2020-04-13T02:02:46.8287324Z 
2020-04-13T02:02:46.8287737Z running 2491 tests
2020-04-13T02:02:55.9747256Z ......iiiii......................................................................................... 100/2491
2020-04-13T02:03:05.0844137Z .....................................................................................ii............. 200/2491
2020-04-13T02:03:25.7030078Z ....................i............................................................................... 400/2491
2020-04-13T02:03:25.7030078Z ....................i............................................................................... 400/2491
2020-04-13T02:03:35.4534612Z ..........................................................................i..i..................iiii 500/2491
2020-04-13T02:03:51.6158020Z .................................................................................................... 700/2491
2020-04-13T02:04:00.3077382Z .................................................................................................... 800/2491
2020-04-13T02:04:08.7520063Z .................................................................................................... 900/2491
2020-04-13T02:04:17.1466632Z .................................................................................................... 1000/2491
---
2020-04-13T02:06:27.6665416Z ---- ptr/mod.rs - ptr::slice_len (line 285) stdout ----
2020-04-13T02:06:27.6665894Z error[E0658]: use of unstable library feature 'ptr_slice_len'
2020-04-13T02:06:27.6666292Z  --> ptr/mod.rs:289:12
2020-04-13T02:06:27.6666437Z   |
2020-04-13T02:06:27.6666609Z 7 | assert_eq!(ptr::slice_len(slice), 3);
2020-04-13T02:06:27.6666977Z   |
2020-04-13T02:06:27.6667212Z   = help: add `#![feature(ptr_slice_len)]` to the crate attributes to enable
2020-04-13T02:06:27.6667413Z 
2020-04-13T02:06:27.6667586Z error: aborting due to previous error
---
2020-04-13T02:06:27.6836632Z 
2020-04-13T02:06:27.6848231Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test --exclude src/tools/tidy
2020-04-13T02:06:27.6848809Z Build completed unsuccessfully in 1:18:32
2020-04-13T02:06:27.6901366Z == clock drift check ==
2020-04-13T02:06:27.6917862Z   local time: Mon Apr 13 02:06:27 UTC 2020
2020-04-13T02:06:27.7151183Z   network time: Mon, 13 Apr 2020 02:06:27 GMT
2020-04-13T02:06:28.1004536Z 
2020-04-13T02:06:28.1004536Z 
2020-04-13T02:06:28.1091844Z ##[error]Bash exited with code '1'.
2020-04-13T02:06:28.1109015Z ##[section]Finishing: Run build
2020-04-13T02:06:28.1168711Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/71082/merge to s
2020-04-13T02:06:28.1173721Z Task         : Get sources
2020-04-13T02:06:28.1174057Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-13T02:06:28.1174382Z Version      : 1.0.0
2020-04-13T02:06:28.1174603Z Author       : Microsoft
2020-04-13T02:06:28.1174603Z Author       : Microsoft
2020-04-13T02:06:28.1174949Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
2020-04-13T02:06:28.1175376Z ==============================================================================
2020-04-13T02:06:28.4666374Z Cleaning any cached credential from repository: rust-lang/rust (GitHub)
2020-04-13T02:06:28.4710525Z ##[section]Finishing: Checkout rust-lang/rust@refs/pull/71082/merge to s
2020-04-13T02:06:28.4805714Z Cleaning up task key
2020-04-13T02:06:28.4807126Z Start cleaning up orphan processes.
2020-04-13T02:06:28.5061474Z Terminate orphan process: pid (3539) (python)
2020-04-13T02:06:28.5239582Z ##[section]Finishing: Finalize Job

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 @rust-lang/infra. (Feature Requests)

@RalfJung
Copy link
Member

Cc #60639

@SimonSapin
Copy link
Contributor

This would also be covered by the more general rust-lang/rfcs#2580. (Please comment there in support if you’d like!)

@neocturne
Copy link
Contributor Author

One question I have is whether we can put this onto slices directly, such that [T]::len is actually implemented with the signature of this method. I'm not sure that would actually in practice be a breaking change (if you have a reference, you definitely have *const [T])...

I did think of this solution and actually tried it first, but as I understand it the function would have to be put in a trait then (I can't add an impl<T> *const [T] block, as the compiler complains that there must only be a single impl for const pointers with the "const_ptr" lang item).

@RalfJung
Copy link
Member

(I can't add an impl *const [T] block, as the compiler complains that there must only be a single impl for const pointers with the "const_ptr" lang item).

This is possible but requires significantly more work, including on the compiler side: #60639 (comment)

@neocturne
Copy link
Contributor Author

(I can't add an impl *const [T] block, as the compiler complains that there must only be a single impl for const pointers with the "const_ptr" lang item).

This is possible but requires significantly more work, including on the compiler side: #60639 (comment)

Looking into it.

@neocturne
Copy link
Contributor Author

neocturne commented Apr 13, 2020

I have pushed an update which implements len() as a method, also introducing the lang items needed for #60639.

For now, I've left the old version in the PR as well - I'll remove those commits if we decide to go for the method.

@oli-obk
Copy link
Contributor

oli-obk commented Apr 14, 2020

This is awesome! The implementation lgtm, but the library method addition should probably be signed off by a libs team member.

r? @SimonSapin or someone else from @rust-lang/libs

This would also be covered by the more general rust-lang/rfcs#2580.

Even so, this new function is imo reasonable to have even if at some point its body is implemented via that rfc.

For now, I've left the old version in the PR as well - I'll remove those commits if we decide to go for the method.

My personal preference is that we should go with the inherent method approach not have a free function at all.

@rust-highfive rust-highfive assigned SimonSapin and unassigned oli-obk Apr 14, 2020
@Mark-Simulacrum
Copy link
Member

I agree that since we can have the inherent method, there's no need for the non-inherent method: the inherent method is strictly better.

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. labels Apr 14, 2020
@Centril Centril added this to the 1.44 milestone Apr 14, 2020
Add lang items for methods on raw slices.
It is already possible to extract the pointer part of a raw slice by a
simple cast, but retrieving the length is not possible without relying
on the representation of the raw slice when it is not valid to convert
the raw slice into a slice reference (i.e. the pointer is null or
unaligned).

Introduce a len() method on raw slices to add this missing feature.
@neocturne neocturne changed the title ptr: introduce slice_len() ptr: introduce len() method on raw slices Apr 14, 2020
@neocturne
Copy link
Contributor Author

neocturne commented Apr 14, 2020

My personal preference is that we should go with the inherent method approach not have a free function at all.

Right, I never intended to have both versions included, I just wanted to give more people a chance to give feedback on the original proposal before removing it.

What are the next steps here? Do I open tracking issues for the unstable features, or should I wait for a review?

@SimonSapin
Copy link
Contributor

@NeoRaider Yes, now that there’s rough consensus for having something like this at all please file a tracking issue and add its number to the #[unstable] attributes.

@Centril Why did you add needs-fcp and relnotes labels? This PR only adds unstable APIs, I’d expect to see those labels in the stabilization PR.

@Centril
Copy link
Contributor

Centril commented Apr 14, 2020

@SimonSapin ah woops, my mistake!

@jonas-schievink jonas-schievink removed needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. labels Apr 14, 2020
@jonas-schievink jonas-schievink removed this from the 1.44 milestone Apr 14, 2020
@SimonSapin
Copy link
Contributor

@bors r=oli-obk,SimonSapin

@bors
Copy link
Contributor

bors commented Apr 14, 2020

📌 Commit dfd6844 has been approved by oli-obk,SimonSapin

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 14, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 15, 2020
Rollup of 8 pull requests

Successful merges:

 - rust-lang#70657 (Allow `try`-blocks in places where an open delim is expected)
 - rust-lang#70947 (tighten CTFE safety net for accesses to globals)
 - rust-lang#70949 (simplify `vec!` macro)
 - rust-lang#71002 (fix target & runtool args order)
 - rust-lang#71082 (ptr: introduce len() method on raw slices)
 - rust-lang#71128 (Remove unused single_step flag)
 - rust-lang#71133 (Tighten time complexity on the doc of sort_by_key)
 - rust-lang#71135 (Update books)

Failed merges:

r? @ghost
@bors bors merged commit 6b8fb7c into rust-lang:master Apr 15, 2020
@neocturne neocturne deleted the ptr_slice_len branch April 17, 2020 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants