Commit Graph

5174 Commits

Author SHA1 Message Date
bors
6b06fdfcd4 Auto merge of #113194 - lu-zero:intrinsics-inline, r=thomcc
Mark wrapped intrinsics as inline(always)

This should mitigate having the inliner decide not to inline when the architecture is lacking an implementation of
TargetTransformInfo::areInlineCompatible aware of the target features (e.g. PowerPC as today).

See https://github.com/rust-lang/stdarch/pull/1443#issuecomment-1613788080
2023-07-01 04:24:26 +00:00
Matthias Krüger
4e8f1357b8
Rollup merge of #113072 - tshepang:patch-1, r=cuviper
str docs: remove "Basic usage" text where not useful

Not "useful" in that there is only one example given
2023-07-01 00:35:04 +02:00
Luca Barbato
528f11c24b Mark wrapped intrinsics as inline(always)
This should mitigate having the inliner decide not to inline when
the architecture is lacking an implementation of
TargetTransformInfo::areInlineCompatible aware of the target
features (e.g. PowerPC as today).
2023-06-30 12:07:21 +02:00
Matthias Krüger
016c306ce6
Rollup merge of #107624 - tgross35:const-cstr-methods, r=dtolnay
Stabilize `const_cstr_methods`

This PR seeks to stabilize `const_cstr_methods`. Fixes most of #101719

## New const stable API

```rust
impl CStr {
    // depends: memchr
    pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError> {...}
    // depends: const_slice_index
    pub const fn to_bytes(&self) -> &[u8] {}
    // depends: pointer casts
    pub const fn to_bytes_with_nul(&self) -> &[u8] {}
    // depends: str::from_utf8
    pub const fn to_str(&self) -> Result<&str, str::Utf8Error> {}
}
```

I don't think any of these methods will have any issue when `CStr` becomes a thin pointer as long as `memchr` is const  (which also allows for const `strlen`) .

## Notes

- `from_bytes_until_nul` relies on `const_slice_index`, which relies on `const_trait_impls`, and generally this should be avoided. After talking with Oli, it should be OK in this case because we could replace the ranges with pointer tricks if needed (worst case being those feature gates disappear). https://github.com/rust-lang/rust/pull/107624#discussion_r1101468480
- Making `from_ptr` const is deferred because it depends on `const_eval_select`. I have moved this under the new flag `const_cstr_from_ptr` https://github.com/rust-lang/rust/pull/107624#discussion_r1101555239

cc ``@oli-obk`` I think you're the const expert

``@rustbot`` modify labels: +T-libs-api +needs-fcp
2023-06-30 08:01:12 +02:00
Matthias Krüger
f35f213d27
Rollup merge of #113054 - Rageking8:make-rustc_on_unimplemented-std-agnostic, r=WaffleLapkin
Make `rustc_on_unimplemented` std-agnostic

See #112923

r? `@WaffleLapkin`
2023-06-29 05:48:40 +02:00
Matthias Krüger
42a495da7e
Rollup merge of #112670 - petrochenkov:typriv, r=eholk
privacy: Type privacy lints fixes and cleanups

See individual commits.
Follow up to https://github.com/rust-lang/rust/pull/111801.
2023-06-29 05:48:39 +02:00
Rageking8
48544c1b12 Make rustc_on_unimplemented std-agnostic 2023-06-27 18:13:24 +08:00
Tshepang Mbambo
6bab85a456
str docs: remove "Basic usage" text where not useful
Not "useful" in that there is only one example given
2023-06-26 22:11:13 +02:00
bors
25b5af1b3a Auto merge of #113024 - Jerrody:master, r=thomcc
`Default`: Always inline primitive data types.
2023-06-26 06:45:04 +00:00
bors
ae8ffa663c Auto merge of #111850 - the8472:external-step-by, r=scottmcm
Specialize `StepBy<Range<{integer}>>`

OLD

    iter::bench_range_step_by_fold_u16      700.00ns/iter +/- 10.00ns
    iter::bench_range_step_by_fold_usize    519.00ns/iter  +/- 6.00ns
    iter::bench_range_step_by_loop_u32      555.00ns/iter  +/- 7.00ns
    iter::bench_range_step_by_sum_reducible  37.00ns/iter  +/- 0.00ns

NEW

    iter::bench_range_step_by_fold_u16       49.00ns/iter +/- 0.00ns
    iter::bench_range_step_by_fold_usize    194.00ns/iter +/- 1.00ns
    iter::bench_range_step_by_loop_u32       98.00ns/iter +/- 0.00ns
    iter::bench_range_step_by_sum_reducible   1.00ns/iter +/- 0.00ns

NEW + `-Ctarget-cpu=x86-64-v3`

    iter::bench_range_step_by_fold_u16      22.00ns/iter +/- 0.00ns
    iter::bench_range_step_by_fold_usize    80.00ns/iter +/- 1.00ns
    iter::bench_range_step_by_loop_u32      41.00ns/iter +/- 0.00ns
    iter::bench_range_step_by_sum_reducible  1.00ns/iter +/- 0.00ns

I have only optimized for walltime of those methods, I haven't tested whether it eliminates bounds checks when indexing into slices via things like `(0..slice.len()).step_by(16)`.
2023-06-26 00:28:30 +00:00
The 8472
f174547124 Mark the StepBy specialization as unsafe 2023-06-25 18:11:51 +02:00
The 8472
8a72f35234 StepBy<Range<{int <= usize}>> can be TrustedLen 2023-06-25 18:11:51 +02:00
The 8472
f70a4b9dd3 doccomments for StepBy specializations 2023-06-25 18:02:11 +02:00
George
bb33730361
Always inline primitive data types. 2023-06-25 12:36:21 +03:00
Matthias Krüger
8168915639
Rollup merge of #112704 - RalfJung:dont-wrap-slices, r=ChrisDenton
slice::from_raw_parts: mention no-wrap-around condition

Cc https://github.com/rust-lang/rust/issues/83996. This probably needs to be mentioned in more places, so I am not closing that issue, but this here should help at least.
2023-06-23 13:18:13 +02:00
The 8472
1bc095cd80 add inline annotation to concrete impls
otherwise they wouldn't be eligible for cross-crate inlining
2023-06-23 00:17:34 +02:00
The 8472
070ce235f2 Specialize StepBy<Range<{integer}>>
For ranges < usize we determine the number of items
StepBy would yield and then store that in the range.end
instead of the actual end. This significantly
simplifies calculation of the loop induction variable
especially in cases where StepBy::step (an usize)
could overflow the Range's item type
2023-06-23 00:17:34 +02:00
Thom Chiovoloni
f978d7ea42
Finish up preliminary tvos support in libstd 2023-06-21 14:59:39 -07:00
bors
006a26c0b5 Auto merge of #111684 - ChayimFriedman2:unused-offset-of, r=WaffleLapkin
Warn on unused `offset_of!()` result

The usage of `core::hint::must_use()` means that we don't get a specialized message. I figured out that since there are plenty of other methods that just have `#[must_use]` with no message it'll be fine, but it is a bit unfortunate that the error mentions `must_use` and not `offset_of!`.

Fixes #111669.
2023-06-21 16:40:54 +00:00
Chayim Refael Friedman
592844cf88 Warn on unused offset_of!() result 2023-06-21 11:43:14 +03:00
bors
6fc0273b5a Auto merge of #112320 - compiler-errors:do-not-impl-via-obj, r=lcnr
Add `implement_via_object` to `rustc_deny_explicit_impl` to control object candidate assembly

Some built-in traits are special, since they are used to prove facts about the program that are important for later phases of compilation such as codegen and CTFE. For example, the `Unsize` trait is used to assert to the compiler that we are able to unsize a type into another type. It doesn't have any methods because it doesn't actually *instruct* the compiler how to do this unsizing, but this is later used (alongside an exhaustive match of combinations of unsizeable types) during codegen to generate unsize coercion code.

Due to this, these built-in traits are incompatible with the type erasure provided by object types. For example, the existence of `dyn Unsize<T>` does not mean that the compiler is able to unsize `Box<dyn Unsize<T>>` into `Box<T>`, since `Unsize` is a *witness* to the fact that a type can be unsized, and it doesn't actually encode that unsizing operation in its vtable as mentioned above.

The old trait solver gets around this fact by having complex control flow that never considers object bounds for certain built-in traits:
2f896da247/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs (L61-L132)

However, candidate assembly in the new solver is much more lovely, and I'd hate to add this list of opt-out cases into the new solver. Instead of maintaining this complex and hard-coded control flow, instead we can make this a property of the trait via a built-in attribute. We already have such a build attribute that's applied to every single trait that we care about: `rustc_deny_explicit_impl`. This PR adds `implement_via_object` as a meta-item to that attribute that allows us to opt a trait out of object-bound candidate assembly as well.

r? `@lcnr`
2023-06-20 08:42:37 +00:00
Michael Goulet
ca68cf0d46 Merge attrs, better validation 2023-06-20 04:38:55 +00:00
Michael Goulet
657d3f43a9 Add rustc_do_not_implement_via_object 2023-06-20 04:38:46 +00:00
Michael Goulet
e24fe97bd9
Rollup merge of #112606 - clarfonthey:ip-display, r=thomcc
Alter `Display` for `Ipv6Addr` for IPv4-compatible addresses

ACP: rust-lang/libs-team#239
2023-06-19 17:53:35 -07:00
Michael Goulet
935452b619
Rollup merge of #112499 - tgross35:py-ruff-fixes, r=Mark-Simulacrum
Fix python linting errors

These were flagged by `ruff`, run using the config in https://github.com/rust-lang/rust/pull/112482
2023-06-19 17:53:34 -07:00
Matthias Krüger
7d072fa8fb
Rollup merge of #112757 - Danvil:patch-1, r=Mark-Simulacrum
Use BorrowFlag instead of explicit isize

The integer type tracking borrow count has a typedef called `BorrowFlag`. This type should be used instead of explicit `isize`.
2023-06-19 19:26:26 +02:00
Matthias Krüger
663fb5a0e9
Rollup merge of #109970 - danielhenrymantilla:add-poll-fn-pin-clarifications, r=thomcc
[doc] `poll_fn`: explain how to `pin` captured state safely

Usage of `Pin::new_unchecked(&mut …)` is dangerous with `poll_fn`, even though the `!Unpin`-infectiousness has made things smoother. Nonetheless, there are easy ways to avoid the need for any `unsafe` altogether, be it through `Box::pin`ning, or the `pin!` macro. Since the latter only works within an `async` context, showing an example artificially introducing one ought to help people navigate this subtlety with safety and confidence.

## Preview

https://user-images.githubusercontent.com/9920355/230092494-da22fdcb-0b8f-4ff4-a2ac-aa7d9ead077a.mov

```@rustbot``` label +A-docs
2023-06-19 19:26:25 +02:00
bors
8d1fa473dd Auto merge of #112724 - scottmcm:simpler-unchecked-shifts, r=Mark-Simulacrum
[libs] Simplify `unchecked_{shl,shr}`

There's no need for the `const_eval_select` dance here.  And while I originally wrote the `.try_into().unwrap_unchecked()` implementation here, it's kinda a mess in MIR -- this new one is substantially simpler, as shown by the old one being above the inlining threshold but the new one being below it in the `mir-opt/inline/unchecked_shifts` tests.

We don't need `u32::checked_shl` doing a dance through both `Result` *and* `Option` 🙃
2023-06-19 04:48:35 +00:00
Daniel Henry-Mantilla
94f7a7931c [doc] poll_fn: explain how to pin captured state safely
Usage of `Pin::new_unchecked(&mut …)` is dangerous with `poll_fn`, even
though the `!Unpin`-infectiousness has made things smoother.
Nonetheless, there are easy ways to avoid the need for any `unsafe`
altogether, be it through `Box::pin`ning, or the `pin!` macro. Since the
latter only works within an `async` context, showing an example
artifically introducing one ought to help people navigate this subtlety
with safety and confidence.
2023-06-18 09:56:13 +00:00
David Weikersdorfer
09707ee12a
Same for BorrowRef 2023-06-18 01:14:45 -07:00
David Weikersdorfer
c4c428b6da
Use BorrowFlag instead of explicit isize
The integer type tracking borrow count has a typedef called `BorrowFlag`. This type should be used instead of explicit `isize`.
2023-06-18 00:46:51 -07:00
bors
0c2c243342 Auto merge of #112599 - saethlin:cleaner-panics, r=thomcc
Launch a non-unwinding panic for misaligned pointer deref

This panic already never unwinds, but that's only because it always hits the unwind guard that's created by our `UnwindAction::Terminate`. Hitting the unwind guard generates a huge double-panic backtrace. Now we generate a normal-looking panic message when this check is hit.

r? `@thomcc`
2023-06-18 01:58:51 +00:00
Matthias Krüger
ba3e535c07
Rollup merge of #112644 - zica87:nonZeroTypes, r=Mark-Simulacrum
Correct types in method descriptions of `NonZero*` types

- `$Int`: e.g. i32, usize
- `$Ty`: e.g. NonZeroI32, NonZeroUsize

|method|current description|after my changes|
|-|-|-|
|`saturating_add`|...Return `$Int`::MAX on overflow.|...Return `$Ty`::MAX on overflow.|
|`checked_abs`|...returns None if self == `$Int`::MIN.|...returns None if self == `$Ty`::MIN.|
|`checked_neg`|...returning None if self == i32::MIN.|...returning None if self == `$Ty`::MIN.|
|`saturating_neg`|...returning MAX if self == i32::MIN...|...returning `$Ty`::MAX if self == `$Ty`::MIN...|
|`saturating_mul`|...Return `$Int`::MAX...|...Return `$Ty`::MAX...|
|`saturating_pow`|...Return `$Int`::MIN or `$Int`::MAX...|...Return `$Ty`::MIN or `$Ty`::MAX...|

---

For example:

```rust
pub const fn saturating_neg(self) -> NonZeroI128
```

- current
  - Saturating negation. Computes `-self`, returning `MAX` if `self == i32::MIN` instead of overflowing.
- after my changes
  - Saturating negation. Computes `-self`, returning `NonZeroI128::MAX` if `self == NonZeroI128::MIN` instead of overflowing.
2023-06-17 12:43:30 +02:00
Trevor Gross
22d00dcd47 Apply changes to fix python linting errors 2023-06-16 20:56:01 -04:00
Scott McMurray
3ec4eeddef [libs] Simplify unchecked_{shl,shr}
There's no need for the `const_eval_select` dance here.  And while I originally wrote the `.try_into().unwrap_unchecked()` implementation here, it's kinda a mess in MIR -- this new one is substantially simpler, as shown by the old one being above the inlining threshold but the new one being below it.
2023-06-16 16:03:19 -07:00
Michael Goulet
38fc6be325
Rollup merge of #112662 - Vanille-N:symbol_unique, r=RalfJung
`#[lang_item]` for `core::ptr::Unique`

Tree Borrows is about to introduce experimental special handling of `core::ptr::Unique` in Miri to give it a semantics.
As of now there does not seem to be a clean way (i.e. other than `&format!("{adt:?}") == "std::ptr::Unique"`) to check if an `AdtDef` represents a `Unique`.

r? `@RalfJung`

Draft: making a lang item
2023-06-16 12:53:22 -07:00
Neven Villani
dc3e91c6c2
#[lang_item] for core::ptr::Unique 2023-06-16 15:22:18 +02:00
Ben Kimock
7a2490eba3 Launch a non-unwinding panic for misaligned pointer deref 2023-06-16 09:20:33 -04:00
Ralf Jung
18b86468b8 slice::from_raw_parts: mention no-wrap-around condition 2023-06-16 14:56:31 +02:00
Guillaume Gomez
d233522418
Rollup merge of #112529 - jieyouxu:block-expr-unused-must-use, r=oli-obk
Extend `unused_must_use` to cover block exprs

Given code like

```rust
#[must_use]
fn foo() -> i32 {
    42
}

fn warns() {
    {
        foo();
    }
}

fn does_not_warn() {
    {
        foo()
    };
}

fn main() {
    warns();
    does_not_warn();
}
```

### Before This PR

```
warning: unused return value of `foo` that must be used
 --> test.rs:8:9
  |
8 |         foo();
  |         ^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
8 |         let _ = foo();
  |         +++++++

warning: 1 warning emitted
```

### After This PR

```
warning: unused return value of `foo` that must be used
 --> test.rs:8:9
  |
8 |         foo();
  |         ^^^^^
  |
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
8 |         let _ = foo();
  |         +++++++

warning: unused return value of `foo` that must be used
  --> test.rs:14:9
   |
14 |         foo()
   |         ^^^^^
   |
help: use `let _ = ...` to ignore the resulting value
   |
14 |         let _ = foo();
   |         +++++++      +

warning: 2 warnings emitted
```

Fixes #104253.
2023-06-15 22:04:56 +02:00
Vadim Petrochenkov
95a24c6ed4 privacy: Do not mark items reachable farther than their nominal visibility
This commit reverts a change made in #111425.
It was believed that this change was necessary for implementing type privacy lints, but #111801 showed that it was not necessary.
Quite opposite, the revert fixes some issues.
2023-06-15 21:25:47 +03:00
Matthias Krüger
c4c5e0baee
Rollup merge of #112621 - GrigorenkoPV:env, r=jyn514
Mention `env!` in `option_env!`'s docs

`env!` mentions that there is an alternative that returns an `Option<...>` instead of emitting a compile error.

Now `option_env!` also mentions that there is an alternative that emits a compile error instead of returning an `Option<...>`.
2023-06-15 17:52:37 +02:00
许杰友 Jieyou Xu (Joe)
72b3b58efc
Extend unused_must_use to cover block exprs 2023-06-15 17:59:13 +08:00
bors
4996b56ba9 Auto merge of #106343 - the8472:slice-iter-fold, r=scottmcm
optimize slice::Iter::fold

Fixes 2 of 4 cases from #106288

```
OLD: test slice::fold_to_last                                           ... bench:         248 ns/iter (+/- 3)
NEW: test slice::fold_to_last                                           ... bench:           0 ns/iter (+/- 0)
```
2023-06-15 09:38:53 +00:00
zica
88b08582b2 Correct types in method descriptions of NonZero* types 2023-06-15 11:41:56 +08:00
The 8472
d90508f761 use indexed loop instead of ptr bumping
this seems to produce less IR
2023-06-14 22:22:41 +02:00
bors
8c74a5d27c Auto merge of #112625 - matthiaskrgr:rollup-jcobj3g, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #112584 (loongarch64-none*: Remove environment component from llvm target)
 - #112600 (Introduce a `Stable` trait to translate MIR to SMIR)
 - #112605 (Improve docs/clean up negative overlap functions)
 - #112611 (Error on unconstrained lifetime in RPITIT)
 - #112612 (Fix explicit-outlives-requirements lint span)
 - #112613 (Fix rustdoc-gui tests on Windows)
 - #112620 (Fix small typo)

r? `@ghost`
`@rustbot` modify labels: rollup
2023-06-14 20:20:40 +00:00
ltdk
2dce58d0f6 Fix SocketAddrV6: Display tests 2023-06-14 15:21:15 -04:00
ltdk
2f2c3f55a9 Fix Ipv6Addr: Display tests 2023-06-14 14:25:25 -04:00
Matthias Krüger
c451f7bedb
Rollup merge of #111974 - Sp00ph:update_guarantees, r=Amanieu
Update runtime guarantee for `select_nth_unstable`

#106933 changed the runtime guarantee for `select_nth_unstable` from O(n) to O(n log n), since the old guarantee wasn't actually met by the implementation at the time. Now with #107522, `select_nth_unstable` should be truly linear in runtime, so we can revert its runtime guarantee to O(n). Since #106933 was considered a bug fix, this will probably need an FCP because it counts as a new API guarantee.

r? `@Amanieu`
2023-06-14 18:10:29 +02:00