Commit graph

135604 commits

Author SHA1 Message Date
bors
aef92d44e4 Auto merge of #80397 - Mark-Simulacrum:fix-bare-tarball, r=pietroalbini
Use package name for top-level directory in bare tarballs

This fixes a bug introduced by #79788.

r? `@pietroalbini`
2020-12-28 08:29:02 +00:00
Konrad Borowski
9e779986aa Add "length" as doc alias to len methods 2020-12-28 09:13:46 +01:00
bors
6c523a7a0e Auto merge of #80422 - RalfJung:weak-no-unsized-raw, r=Mark-Simulacrum
de-stabilize unsized raw ptr methods for Weak

`@Mark-Simulacrum` this is the patch re: https://github.com/rust-lang/rust/pull/80407.

I couldn't figure out the branch it needs to go on though, stable is still the old stable but beta already the new beta...?
2020-12-28 00:00:43 +00:00
Ralf Jung
8e0b7f988e de-stabilize unsized raw ptr methods for Weak 2020-12-28 00:39:09 +01:00
Camelid
cdad0c80ef
Also show the displayed version of IntVar and FloatVar
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
2020-12-27 11:05:40 -08:00
bors
257becbfe4 Auto merge of #80181 - jyn514:intra-doc-primitives, r=Manishearth
Fix intra-doc links for non-path primitives

This does *not* currently work for associated items that are
auto-implemented by the compiler (e.g. `never::eq`), because they aren't
present in the source code. I plan to fix this in a follow-up PR.

Fixes https://github.com/rust-lang/rust/issues/63351 using the approach mentioned in https://github.com/rust-lang/rust/issues/63351#issuecomment-683352130.

r? `@Manishearth`

cc `@petrochenkov` - this makes `rustc_resolve::Res` public, is that ok? I'd just add an identical type alias in rustdoc if not, which seems a waste.
2020-12-27 18:55:33 +00:00
LeSeulArtichaut
fcc88fab6c Add regression test for #80375 2020-12-27 19:34:33 +01:00
bors
76188b6830 Auto merge of #80315 - tmiasko:ignore-proc-macros, r=Mark-Simulacrum
Ignore proc-macros when assembling rustc libdir

Fixes #80294.
2020-12-27 16:03:01 +00:00
Vadim Petrochenkov
0ae998e122 rustc_span: Remove Symbol::with 2020-12-27 18:10:58 +03:00
bors
2fab321435 Auto merge of #79134 - ohadravid:nzint-div, r=dtolnay
Add `impl Div<NonZeroU{0}> for u{0}` which cannot panic

Dividing an unsigned int by a `NonZeroUxx` requires a user to write (for example, in [this SO question](https://stackoverflow.com/questions/64855738/how-to-inform-the-optimizer-that-nonzerou32get-will-never-return-zero)):

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y.get()
}
```

which generates a panicking-checked-div [assembly](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(fontScale:14,j:1,lang:rust,selection:(endColumn:2,endLineNumber:6,positionColumn:2,positionLineNumber:6,selectionStartColumn:2,selectionStartLineNumber:6,startColumn:2,startLineNumber:6),source:%27pub+fn+div(x:+u32,+y:+u32)+-%3E+u32+%7B%0A++++x+/+y%0A%7D%0Apub+fn+safe_div(x:+u32,+y:+std::num::NonZeroU32)+-%3E+u32+%7B%0A++++x+/+y.get()+//+an+unchecked+division+expected%0A%7D%27),l:%275%27,n:%270%27,o:%27Rust+source+%231%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27),(g:!((h:compiler,i:(compiler:r1470,filters:(b:%270%27,binary:%271%27,commentOnly:%270%27,demangle:%270%27,directives:%270%27,execute:%271%27,intel:%270%27,libraryCode:%271%27,trim:%271%27),fontScale:14,j:1,lang:rust,libs:!(),options:%27-O%27,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:%275%27,n:%270%27,o:%27rustc+1.47.0+(Editor+%231,+Compiler+%231)+Rust%27,t:%270%27)),k:50,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27)),l:%272%27,n:%270%27,o:%27%27,t:%270%27)),version:4).
Avoiding the `panic` currently requires `unsafe` code.

This PR adds an `impl Div<NonZeroU{0}> for u{0}` (and `impl Rem<NonZeroU{0}> for u{0}`) which calls the `unchecked_div` (and `unchecked_rem`) intrinsic without any additional checks,
making the following code compile:

```
pub fn safe_div(x: u32, y: std::num::NonZeroU32) -> u32 {
    x / y
}

pub fn safe_rem(x: u32, y: std::num::NonZeroU32) -> u32 {
    x % y
}
```

The doc is set to match the regular div impl [docs](https://doc.rust-lang.org/beta/src/core/ops/arith.rs.html#460).

I've marked these as stable because (as I understand it) trait impls are automatically stable. I'm happy to change it to unstable if needed.

Following `@dtolnay` template from a similar issue:
this adds the following **stable** impls, which rely on dividing unsigned integers by nonzero integers being well defined and previously would have involved unsafe code to encode that knowledge:
```
impl Div<NonZeroU8> for u8 {
    type Output = u8;
}

impl Rem<NonZeroU8> for u8 {
    type Output = u8;
}
```
and equivalent for u16, u32, u64, u128, usize, but **not** for i8, i16, i32, i64, i128, isize (since -1/MIN is undefined).

r? `@dtolnay`
2020-12-27 13:11:06 +00:00
Marco Ieni
f57152f640
rustdoc book: fix example 2020-12-27 14:10:00 +01:00
David Tolnay
9586643912
Bump nonzero_div feature to Rust 1.51 2020-12-27 03:17:30 -08:00
bors
dc6121ca68 Auto merge of #79642 - ijackson:default-theme-stab, r=jyn514
rustdoc: stabilise --default-theme command line option

As discussed in #77213, this seems like it has bedded in and can be safely and usefully made stable.

(rustdoc already has other stable options that interact quite intimately with the rustdoc-supplied CSS, and also an option for supplying entirely different CSS, so exposing the theme names this way seems a very minor step.)

There is also a commit to do some minor grammar fixes to the help message.
2020-12-27 09:55:51 +00:00
bjorn3
52cf01c815 Merge commit 'dbee13661efa269cb4cd57bb4c6b99a19732b484' into sync_cg_clif-2020-12-27 2020-12-27 10:30:38 +01:00
bors
1d517afcd0 Auto merge of #79135 - lcnr:the-paleogenesis-of-generic-germination, r=varkor
stabilize `#![feature(min_const_generics)]` in 1.51

*A new Kind*
*A Sort long Prophesized*
*Once Fragile, now Eternal*

blocked on #79073.

# Stabilization report

This is the stabilization report for `#![feature(min_const_generics)]` (tracking issue #74878), a subset of `#![feature(const_generics)]` (tracking issue #44580), based on rust-lang/rfcs#2000.

The [version target](https://forge.rust-lang.org/#current-release-versions) is ~~1.50 (2020-12-31 => beta, 2021-02-11 => stable)~~ 1.51 (2021-02-111 => beta, 2021-03-25 => stable).

This report is a collaborative effort of `@varkor,` `@shepmaster` and `@lcnr.`

## Summary

It is currently possible to parameterize functions, type aliases, types, traits and implementations by types and lifetimes.
With `#![feature(min_const_generics)]`, it becomes possible, in addition, to parameterize these by constants.

This is done using the syntax `const IDENT: Type` in the parameter listing. Unlike full const generics, `min_const_generics` is limited to parameterization by integers, and constants of type `char` or `bool`.

We already use `#![feature(min_const_generics)]` on stable to implement many common traits for arrays. See [the documentation](https://doc.rust-lang.org/nightly/std/primitive.array.html) for specific examples.

Generic const arguments, for now, are not permitted to involve computations depending on generic parameters. This means that const parameters may only be instantiated using either:

1. const expressions that do not depend on any generic parameters, e.g. `{ foo() + 1 }`, where `foo` is a `const fn`
1. standalone const parameters, e.g. `{N}`

### Example

```rust
#![feature(min_const_generics)]

trait Foo<const N: usize> {
    fn method<const M: usize>(&mut self, arr: [[u8; M]; N]);
}

struct Bar<T, const N: usize> {
    inner: [T; N],
}

impl<const N: usize> Foo<N> for Bar<u8, N> {
    fn method<const M: usize>(&mut self, arr: [[u8; M]; N]) {
        for (elem, s) in self.inner.iter_mut().zip(arr.iter()) {
            for &x in s {
                *elem &= x;
            }
        }
    }
}

fn function<const N: u16>() -> u16 {
    // Const parameters can be used freely inside of functions.
    (N + 1) / 2 * N
}

fn main() {
    let mut bar = Bar { inner: [0xff; 3] };
    // This infers the value of `M` from the type of the function argument.
    bar.method([[0b11_00, 0b01_00], [0b00_11, 0b00_01], [0b11_00, 0b00_11]]);
    assert_eq!(bar.inner, [0b01_00, 0b00_01, 0b00_00]);

    // You can also explicitly specify the value of `N`.
    assert_eq!(function::<17>(), 153);
}
```

## Motivation

Rust has the built-in array type, which is parametric over a constant. Without const generics, this type can be quite cumbersome to use as it is not possible to generically implement a trait for arrays of different lengths. For example, this meant that, for a long time, the standard library only contained trait implementations for arrays up to a length of 32. This restriction has since been lifted through the use of const generics.

Const parameters allow users to naturally specify variants of a generic type which are more naturally parameterized by values, rather than by types. For example, using const generics, many of the uses of the crate [typenum](https://crates.io/crates/typenum) may now be replaced with const parameters, improving compilation time as well as code readability and diagnostics.

The subset described by `min_const_generics` is self-contained, but extensive enough to help with the most frequent issues: implementing traits for arrays and using arbitrarily-sized arrays inside of other types. Furthermore, it extends naturally to full `const_generics` once the remaining design and implementation questions have been resolved.

## In-depth feature description

### Declaring const parameters

*Const parameters* are allowed in all places where types and lifetimes are supported. They use the syntax `const IDENT: Type`. Currently, const parameters must be declared after lifetime and type parameters. Their scope is equal to the scope of other generic parameters. They live in the value namespace.

`Type` must be one of `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `char` and `bool`. This restriction is implemented in two places:

1. during name resolution, where we forbid generic parameters
1. during well-formedness checking, where we only allow the types listed above

The updated syntax of parameter listings is:

```
GenericParams:
    (OuterAttr* LifetimeParam),* (OuterAttr* TypeParam),* (OuterAttr* ConstParam),*

OuterAttr: '#[' ... ']'
LifetimeParam: ...
TypeParam: ...
ConstParam: 'const' IDENT ':' Type
```

Unlike type and lifetime parameters, const parameters of types can be used without being mentioned inside of a parameterized type because const parameters do not have issues concerning variance. This means that the following types are allowed:

```rust
struct Foo<const N: usize>;
enum Bar<const M: usize> { A, B }
```

### Const arguments

Const parameters are instantiated using *const arguments*. Any concrete const expression or const parameter as a standalone argument can be used. When applying an expression as const parameter, most expressions must be contained within a block, with two exceptions:

1. literals and single-segment path expressions
1. array lengths

This syntactic restriction is necessary to avoid ambiguity, or requiring infinite lookahead when parsing an expression as a generic argument.

In the cases where a generic argument could be resolved as either a type or const argument, we always interpret it as a type. This causes the following test to fail:

```rust
type N = u32;
struct Foo<const N: usize>;
fn foo<const N: usize>() -> Foo<N> { todo!() } // ERR
```

To circumvent this, the user may wrap the const parameter with braces, at which point it is unambiguously accepted.

```rust
type N = u32;
struct Foo<const N: usize>;
fn bar<const N: usize>() -> Foo<{ N }> { todo!() } // ok
```

Operations depending on generic parameters are **not** allowed, which is enforced during well-formedness checking. Allowing generic unevaluated constants would require a way to check if they would always evaluate successfully to prevent errors that are not caught at declaration time. This ability forms part of `#![feature(const_evaluatable_checked)]`, which is not yet being stabilised.

Since we are not yet stabilizing `#![feature(lazy_normalization_consts)]`, we must not supply the parent generics to anonymous constants except for repeat expressions. Doing so can cause cycle errors for arrays used in `where`-bounds. Not supplying the parent generics can however lead to ICEs occurring before well-formedness checking when trying to use a generic parameter. See #56445 for details.

Since we expect cases like this to occur more frequently once `min_const_generics` is stabilized, we have chosen to forbid generic parameters in anonymous constants during name resolution. While this changes the ICE in the situation above to an ordinary error, this is theoretically a breaking change, as early-bound lifetimes were previously permitted in repeat expressions but now are disallowed, causing the following snippet to break:

```rust
fn late_bound<'a>() {
    let _ = [0; {
        let _: &'a (); // ICE ==> ERR
        3
    }];
}

fn early_bound<'a>() where &'a (): Sized {
    let _ = [0; {
        let _: &'a (); // ok ==> ERR
        3
    }];
}
```

### Using const parameters

Const parameters can be used almost everywhere ordinary constants are allowed, except that they may not be used in the construction of consts, statics, functions, or types inside a function body and are subject to the generic argument restrictions mentioned above.

Expressions containing const parameters are eligible for promotion:

```rust
fn test<const N: usize>() -> &'static usize {
    &(3 + N)
}
```

### Symbol mangling

See the [Rust symbol name mangling RFC](https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html) for an overview. Generic const parameters take the form `K[type][value]` when the value is known, or `Kp` where the value is not known, where:
- `[type]` is any integral type, `bool`, or `char`.
- `[value]` is the unsigned hex value for integers, preceded by `n` when negative; is `0` or `1` for `bool`; is the hex value for `char`.

### Exhaustiveness checking

We do not check the exhaustiveness of impls, meaning that the following example does **not** compile:

```rust
struct Foo<const B: bool>;
trait Bar {}
impl Bar for Foo<true> {}
impl Bar for Foo<false> {}

fn needs_bar(_: impl Bar) {}
fn generic<const B: bool>() {
    let v = Foo::<B>;
    needs_bar(v);
}
```

### Type inference

The value of const parameters can be inferred during typeck. One interesting case is the length of generic arrays, which can also be inferred from patterns (implemented in #70562). Practical usage of this can be seen in #76825.

### Equality of constants

`#![feature(min_const_generics)]` only permits generic parameters to be used as standalone generic arguments. We compare two parameters to be equal if they are literally the same generic parameter.

### Associated constants

Associated constants can use const parameters without restriction, see https://github.com/rust-lang/rust/pull/79135#issuecomment-748299774 for more details.

## Future work

As this is a limited subset of rust-lang/rfcs#2000, there are quite a few extensions we will be looking into next.

### Lazy normalization of constants

Stabilizing `#![feature(lazy_normalization_consts)]` (tracking issue #72219) will remove some special cases that are currently necessary for `min_const_generics`, and unblocks operations on const parameters.

### Relaxing ordering requirements between const and type parameters

We currently restrict the order of generic parameters so that types must come before consts. We could relax this, as is currently done with `const_generics`. Without this it is not possible to use both type defaults and const parameters at the same time.

Unrestricting the order will require us to improve some diagnostics that expect there to be a strict order between type and const parameters.

### Allowing more parameter types

We would like to support const parameters of more types, especially`&str` and user-defined types. Both are blocked on [valtrees]. There are also open questions regarding the design of `structural_match` concerning the latter. Supporting generic const parameter types such as `struct Foo<T, const N: T>` will be a lot harder and is unlikely to be implemented in the near future.

### Default values of const parameters

We do not yet support default values for const parameters. There is work in progress to enable this on nightly (see https://github.com/rust-lang/rust/pull/75384).

### Generic const operations

With `#![feature(min_const_generics)]`, only concrete const expressions and parameters as standalone arguments are allowed in types and repeat expressions. However, supporting generic const operations, such as `N + 1` or `std::mem::size_of::<T>()` is highly desirable. This feature is in early development under `#![feature(const_evaluatable_checked)]`.

## Implementation history

Many people have contributed to the design and implementation of const generics over the last three years. See https://github.com/rust-lang/rust/issues/44580#issuecomment-728913127 for a summary. Once again thank you to everybody who helped out here!

[valtrees]: https://github.com/rust-lang/rust/issues/72396

---

r? `@varkor`
2020-12-27 07:05:56 +00:00
bors
0f42d47bd5 Auto merge of #80400 - adlerd:hashclone, r=Mark-Simulacrum
Use `clone_from` from `hashbrown::{HashMap,HashSet}`.

This change updates the `std` hash collections to use `hashbrown`'s `clone_from`, which was itself added in #70052. Deriving `Clone` does not add a `clone_from` impl and uses the trait default, which calls `clone`.

Fixes #28481
2020-12-27 04:14:20 +00:00
0xflotus
cb177852c1
fix: small typo error in chalk/mod.rs 2020-12-27 03:22:23 +01:00
Camelid
6aea014fbf
Document InferTy & co. 2020-12-26 17:55:39 -08:00
bors
7e0246552f Auto merge of #80395 - ehuss:lint-docs-warn-missing, r=Mark-Simulacrum
lint-docs: Warn on missing lint when documenting.

In #79522, I missed converting one of the errors to a warning, in the situation where a lint is missing.  This was unearthed by the renaming of overlapping-patterns (#78242).  This will still be validated during the test phase.
2020-12-27 01:21:40 +00:00
David Adler
7adeb710fb Use the hashbrown::{HashMap,HashSet} clone_from impls. 2020-12-26 19:39:38 -05:00
Joshua Nelson
53da233aec Remove FIXME in rustc_privacy 2020-12-26 19:34:10 -05:00
CAD97
eeed3118d0 Use raw version of align_of in rc data_offset
This was missed in #73845 when switching to use the raw operators.
Fixes #80365
2020-12-26 18:37:07 -05:00
bors
0fbc0ce3ab Auto merge of #80396 - ehuss:fix-missing-deny, r=Mark-Simulacrum
Fix missing deny-by-default.md file.

I don't know why, but #80296 deleted this file.  Add it back so that the docs can be viewed directly with mdbook without it auto-generating the file.
2020-12-26 22:35:16 +00:00
Mark Rousskov
fe52a65186 Use package name for top-level directory in bare tarballs
This fixes a bug introduced by #79788.
2020-12-26 17:15:20 -05:00
Eric Huss
6dab9265c8 Fix missing deny-by-default.md file. 2020-12-26 13:53:10 -08:00
Eric Huss
966621563e lint-docs: Warn on missing lint when documenting. 2020-12-26 13:48:09 -08:00
Eric Huss
142764f79e Add links to the source for the rustc and rustdoc books. 2020-12-26 13:22:33 -08:00
Stein Somers
be3a5a1bec BTreeMap: rename the area access methods 2020-12-26 21:43:04 +01:00
bors
0b644e4196 Auto merge of #79045 - oli-obk:dont_rely_on_alloc_happening_for_soundness, r=TimDiekmann
Document that heap allocations are not guaranteed to happen, even if explicitly performed in the code

cc `@RalfJung`
2020-12-26 19:43:12 +00:00
Bastian Kauschke
c4ba60a191 update tests 2020-12-26 18:24:10 +01:00
Bastian Kauschke
18fa7789cd update error codes 2020-12-26 18:24:10 +01:00
Bastian Kauschke
49f890bdb3 delete no longer relevant tests 2020-12-26 18:24:10 +01:00
Bastian Kauschke
06cc9c26da stabilize min_const_generics 2020-12-26 18:24:10 +01:00
oli
efcd8a96c4 DIrect invocations of AllocRef::alloc cannot get optimized away 2020-12-26 17:16:50 +00:00
oli
fba17e3f8d Adjust markdown text to be more like the rendered text 2020-12-26 17:14:49 +00:00
Ralf Jung
8543388beb address review feedback 2020-12-26 18:07:52 +01:00
Oli Scherer
714feab059
Update library/core/src/alloc/mod.rs
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-12-26 18:06:04 +01:00
Oli Scherer
48c8ff59ec
Update library/core/src/alloc/global.rs
Co-authored-by: Ralf Jung <post@ralfj.de>
2020-12-26 18:05:55 +01:00
bors
89524d0f8e Auto merge of #79520 - ssomers:btree_cleanup_1, r=Mark-Simulacrum
BTreeMap: clean up access to MaybeUninit arrays

Stop exposing and using immutable access to `MaybeUninit` slices when we need and have exclusive access to the tree.

r? `@Mark-Simulacrum`
2020-12-26 16:47:33 +00:00
Ralf Jung
3533204e99 clarify wrapping ptr arithmetic docs 2020-12-26 15:59:02 +01:00
bors
30a42735a0 Auto merge of #80354 - ssomers:btree_test_compact, r=Mark-Simulacrum
BTreeMap: test full nodes a little more

r? `@Mark-Simulacrum`
2020-12-26 13:46:16 +00:00
bors
1f5beec3b1 Auto merge of #80316 - ehuss:rustdoc-index, r=Mark-Simulacrum
Include rustdoc in the compiler docs index.

This should help ensure that the index page at https://doc.rust-lang.org/nightly/nightly-rustc/ includes a link to the rustdoc docs as well.

Fixes #80307
2020-12-26 09:29:56 +00:00
bors
780b094d76 Auto merge of #80209 - erikdesjardins:ptrcmp, r=Mark-Simulacrum
Remove pointer comparison from slice equality

This resurrects #71735.

Fixes #71602, helps with #80140.

r? `@Mark-Simulacrum`
2020-12-26 06:43:51 +00:00
bors
733cb54d18 Remove pointer comparison from slice equality
This resurrects #71735.

Fixes #71602, helps with #80140.

r? `@Mark-Simulacrum`
2020-12-26 06:43:51 +00:00
bors
d30dac2d83 Auto merge of #79022 - SpyrosRoum:stabilize-deque_range, r=m-ou-se
stabilize deque_range

Make #74217 stable, stabilizing `VecDeque::range` and `VecDeque::range_mut`.
Pr: #74099

r? `@m-ou-se`
2020-12-26 03:50:16 +00:00
bors
931aa27922 Auto merge of #80246 - matthewjasper:projection-cycle-caching, r=Mark-Simulacrum
Prevent caching normalization results with a cycle

When normalizing a projection which results in a cycle, we would cache the result of `project_type` without the nested obligations (because they're not needed for inference). This would result in the nested obligations only being handled once in fulfill, which would avoid the cycle error. `get_paranoid_cache_value_obligation` used to add an obligation that resulted in a cycle in this case previously, but was removed by #73905.

This PR makes the projection cache not cache the value of a projection if it was ever normalized in a cycle (except in a snapshot that's rolled back).

Fixes #79714.

r? `@nikomatsakis`
2020-12-26 00:11:30 +00:00
bors
1d2730f413 Auto merge of #80366 - Mark-Simulacrum:bump-version, r=Mark-Simulacrum
Bump version to 1.51

r? `@Mark-Simulacrum`
2020-12-25 21:19:11 +00:00
bors
bb178237c5 Auto merge of #80235 - RalfJung:validate-promoteds, r=oli-obk
validate promoteds

Turn on const-value validation for promoteds. This is made possible now that https://github.com/rust-lang/rust/issues/67534 is resolved.

I don't think this is a breaking change. We don't promote any unsafe operation any more (since https://github.com/rust-lang/rust/pull/77526 landed). We *do* promote `const fn` calls under some circumstances (in `const`/`static` initializers), but union field access and similar operations are not allowed in `const fn`. So now is a perfect time to add this check. :D

r? `@oli-obk`
Fixes https://github.com/rust-lang/rust/issues/67465
2020-12-25 18:25:48 +00:00
Mark Rousskov
369449c89b Bump version to 1.51 2020-12-25 10:42:53 -05:00
bors
1832bdd7de Auto merge of #80296 - wesleywiser:revert_missing_fragment_specifier_hard_error, r=Mark-Simulacrum
Revert missing fragment specifier hard error

Closes #76605

Reopens #40107

r? `@Mark-Simulacrum`
2020-12-25 14:09:08 +00:00