Commit Graph

243 Commits

Author SHA1 Message Date
Nadrieril 1fab669f8d Be honest about being able to list constructors
The test change is because we used to treat `&str` like other `&T`s, ie
as having a single constructor. That's not quite true though since we
consider `&str` constants as atomic instead of refs to `str` constants.
2020-10-27 03:09:55 +00:00
Nadrieril db9a8480c4 Simplify specialize_constructor
Also removes the ugly caching that was introduced in #76918. It was
bolted on without deeper knowledge of the workings of the algorithm.
This commit manages to be more performant without any of the complexity.
It should be better on representative workloads too.
2020-10-27 03:08:38 +00:00
Nadrieril 54fa70290d Unify the paths through `is_useful` 2020-10-27 00:49:32 +00:00
Nadrieril c96bd28ab3 Recompute `MissingConstructors` when needed
This only happens in a slow (diagnostics) path, so the code clarity gain
is worth it.
2020-10-27 00:46:33 +00:00
Nadrieril b49f90760d Pass more things through `PatCtxt`
This is even a perf improvement on the match-heavy benchmarks.
2020-10-27 00:46:33 +00:00
Nadrieril cdafd1e1bd Let MissingConstructors handle the subtleties of missing constructors 2020-10-27 00:46:32 +00:00
Nadrieril 1190e7275c Cache head constructor in PatStack
Since the constructor is recomputed a lot, caching is worth it.
2020-10-27 00:46:32 +00:00
Nadrieril 833089fbc9 Unify the two kinds of specialization by adding a Wildcard ctor 2020-10-27 00:46:32 +00:00
Nadrieril 41e7ca499d Inline `specialize_one_pattern` 2020-10-27 00:46:32 +00:00
Nadrieril c511955a9f Factor out the two specialization steps 2020-10-27 00:46:32 +00:00
Nadrieril 6ad9f44a50 Clarify specialization into two steps
First is checking for constructor overlap, second is extracting the
resulting fields.
2020-10-27 00:46:32 +00:00
Nadrieril 7c4f94be48 Use pat_constructor to simplify specialize_one_pattern 2020-10-27 00:46:32 +00:00
Nadrieril feb1e13960 Split `split_grouped_constructor` into smaller functions 2020-10-27 00:46:32 +00:00
Yuki Okushi b72d70ef61
Rollup merge of #78377 - LeSeulArtichaut:patch-docs, r=jonas-schievink
Fix typo in debug statement
2020-10-27 08:45:14 +09:00
bors 0da6d42f29 Auto merge of #68965 - eddyb:mir-inline-scope, r=nagisa,oli-obk
rustc_mir: track inlined callees in SourceScopeData.

We now record which MIR scopes are the roots of *other* (inlined) functions's scope trees, which allows us to generate the correct debuginfo in codegen, similar to what LLVM inlining generates.
This PR makes the `ui` test `backtrace-debuginfo` pass, if the MIR inliner is turned on by default.

Also, `#[track_caller]` is now correct in the face of MIR inlining (cc `@anp).`

Fixes #76997.

r? `@rust-lang/wg-mir-opt`
2020-10-26 18:50:22 +00:00
LeSeulArtichaut 9de15188b0 Fix typo in debug statement 2020-10-25 20:54:44 +01:00
Jonas Schievink e12e97223f
Rollup merge of #78072 - Nadrieril:cleanup-constant-matching, r=varkor
Cleanup constant matching in exhaustiveness checking

This supercedes https://github.com/rust-lang/rust/pull/77390. I made the `Opaque` constructor work.
I have opened two issues https://github.com/rust-lang/rust/issues/78071 and https://github.com/rust-lang/rust/issues/78057 from the discussion we had on the previous PR. They are not regressions nor directly related to the current PR so I thought we'd deal with them separately.

I left a FIXME somewhere because I didn't know how to compare string constants for equality. There might even be some unicode things that need to happen there. In the meantime I preserved previous behavior.

EDIT: I accidentally fixed #78071
2020-10-24 22:39:51 +02:00
Jonas Schievink a547055184
Rollup merge of #76614 - NoraCodes:nora/control_flow_enum, r=scottmcm
change the order of type arguments on ControlFlow

This allows ControlFlow<BreakType> which is much more ergonomic for common iterator combinator use cases.

Addresses one component of #75744
2020-10-24 22:39:41 +02:00
Yuki Okushi 709de7817d
Rollup merge of #78098 - camelid:fixup-docs, r=steveklabnik
Clean up and improve some docs

* compiler docs
  * Don't format list as part of a code block
  * Clean up some other formatting
* rustdoc book
  * Update CommonMark spec version to latest (0.28 -> 0.29)
  * Clean up some various wording and formatting
2020-10-23 18:26:28 +09:00
Leonora Tindall bc2317915f Don't re-export std::ops::ControlFlow in the compiler. 2020-10-22 17:26:55 -07:00
Camelid d725da129e Clean up and improve some docs
* compiler docs
  * Don't format list as part of a code block
  * Clean up some other formatting
* rustdoc book
  * Update CommonMark spec version to latest (0.28 -> 0.29)
  * Clean up some various wording and formatting
2020-10-21 18:01:04 -07:00
Nadrieril faf87105db Explain the `Opaque` special case in specialization 2020-10-21 20:15:02 +01:00
bishtpawan 7f5847735a Fix formatting 2020-10-21 11:42:52 +05:30
Eduard-Mihai Burtescu 6451b39a25 rustc_mir: support MIR-inlining #[track_caller] functions. 2020-10-21 04:43:56 +03:00
Eduard-Mihai Burtescu 6bc5eafbce rustc_mir: track inlined callees in SourceScopeData. 2020-10-21 04:43:56 +03:00
bishtpawan fa094044a9 Fix build failure of rustfmt 2020-10-20 23:13:21 +05:30
Nadrieril c4ae6c2bb9 Add comment 2020-10-18 17:05:19 +01:00
Nadrieril da0ba2f645 The only remaining constant patterns are opaque 2020-10-18 14:21:20 +01:00
Nadrieril d1a784e7b9 Treat string literals separately from other constants 2020-10-18 14:21:20 +01:00
Nadrieril aa4172076e Handle ranges of float consistently
This deconfuses the comparison of floats, that currently mixed ranges
and non-ranges.
2020-10-18 14:21:20 +01:00
Nadrieril f504e9a425 Fix comment 2020-10-18 14:21:20 +01:00
Oliver Scherer 3708c86de1 Treat booleans as integers with valid range 0..=1 2020-10-18 14:21:20 +01:00
Nadrieril 99852e0db6 A ConstantValue constructor with a slice pattern is an error 2020-10-18 14:21:20 +01:00
Oliver Scherer c3d0445021 Destructure byte slices and remove all the workarounds 2020-10-18 14:21:20 +01:00
Oliver Scherer bb8111069e Destructure byte array constants to array patterns instead of keeping them opaque 2020-10-18 14:21:20 +01:00
bors 6af9846fcc Auto merge of #77124 - spastorino:const-exprs-rfc-2920, r=oli-obk
Implement const expressions and patterns (RFC 2920)

cc `@ecstatic-morse` `@lcnr` `@oli-obk` `@petrochenkov`
2020-10-17 14:44:51 +00:00
Tomasz Miąsko a65a283332 Remove unused cached_unreachable_block from MIR builder 2020-10-17 00:00:00 +00:00
Santiago Pastorino fe922e567f
Lower inline const down to MIR 2020-10-16 15:21:18 -03:00
Dylan DPC 85dbb03490
Rollup merge of #76119 - Amjad50:stabilizing-move_ref_pattern, r=nikomatsakis
Stabilize move_ref_pattern

# Implementation
- Initially the rule was added in the run-up to 1.0. The AST-based borrow checker was having difficulty correctly enforcing match expressions that combined ref and move bindings, and so it was decided to simplify forbid the combination out right.
- The move to MIR-based borrow checking made it possible to enforce the rules in a finer-grained level, but we kept the rule in place in an effort to be conservative in our changes.
- In #68376, @Centril lifted the restriction but required a feature-gate.
- This PR removes the feature-gate.

Tracking issue: #68354.

# Description
This PR is to stabilize the feature `move_ref_pattern`, which allows patterns
containing both `by-ref` and `by-move` bindings at the same time.

For example: `Foo(ref x, y)`, where `x` is `by-ref`,
and `y` is `by-move`.

The rules of moving a variable also apply here when moving *part* of a variable,
such as it can't be referenced or moved before.

If this pattern is used, it would result in *partial move*, which means that
part of the variable is moved. The variable that was partially moved from
cannot be used as a whole in this case, only the parts that are still
not moved can be used.

## Documentation
- The reference (rust-lang/reference#881)
- Rust by example (rust-lang/rust-by-example#1377)

## Tests
There are many tests, but I think one of the comperhensive ones:
- [borrowck-move-ref-pattern-pass.rs](85fbf49ce0/src/test/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern-pass.rs)
- [borrowck-move-ref-pattern.rs](85fbf49ce0/src/test/ui/pattern/move-ref-patterns/borrowck-move-ref-pattern.rs)

# Examples

```rust
#[derive(PartialEq, Eq)]
struct Finished {}

#[derive(PartialEq, Eq)]
struct Processing {
    status: ProcessStatus,
}

#[derive(PartialEq, Eq)]
enum ProcessStatus {
    One,
    Two,
    Three,
}

#[derive(PartialEq, Eq)]
enum Status {
    Finished(Finished),
    Processing(Processing),
}

fn check_result(_url: &str) -> Status {
    // fetch status from some server
    Status::Processing(Processing {
        status: ProcessStatus::One,
    })
}

fn wait_for_result(url: &str) -> Finished {
    let mut previous_status = None;
    loop {
        match check_result(url) {
            Status::Finished(f) => return f,
            Status::Processing(p) => {
                match (&mut previous_status, p.status) {
                    (None, status) => previous_status = Some(status), // first status
                    (Some(previous), status) if *previous == status => {} // no change, ignore
                    (Some(previous), status) => { // Now it can be used
                        // new status
                        *previous = status;
                    }
                }
            }
        }
    }
}
```

Before, we would have used:
```rust
                match (&previous_status, p.status) {
                    (Some(previous), status) if *previous == status => {} // no change, ignore
                    (_, status) => {
                        // new status
                        previous_status = Some(status);
                    }
                }
```

Demonstrating *partial move*
```rust
fn main() {
    #[derive(Debug)]
    struct Person {
        name: String,
        age: u8,
    }

    let person = Person {
        name: String::from("Alice"),
        age: 20,
    };

    // `name` is moved out of person, but `age` is referenced
    let Person { name, ref age } = person;

    println!("The person's age is {}", age);

    println!("The person's name is {}", name);

    // Error! borrow of partially moved value: `person` partial move occurs
    //println!("The person struct is {:?}", person);

    // `person` cannot be used but `person.age` can be used as it is not moved
    println!("The person's age from person struct is {}", person.age);
}
```
2020-10-16 02:10:07 +02:00
bors f243a2ad90 Auto merge of #77917 - JohnTitor:rollup-e47h2qt, r=JohnTitor
Rollup of 14 pull requests

Successful merges:

 - #77239 (Enable building Cargo for aarch64-apple-darwin)
 - #77569 (BTreeMap: type-specific variants of node_as_mut and cast_unchecked)
 - #77719 (Remove unnecessary rustc_const_stable attributes.)
 - #77722 (Remove unsafety from sys/unsupported and add deny(unsafe_op_in_unsafe_fn).)
 - #77725 (Add regression issue template)
 - #77776 ( Give an error when running `x.py test --stage 0 src/test/ui`)
 - #77786 (Mention rustdoc in `x.py setup`)
 - #77825 (`min_const_generics` diagnostics improvements)
 - #77868 (Include `llvm-dis`, `llc` and `opt` in `llvm-tools-preview` component)
 - #77884 (Use Option::unwrap_or instead of open-coding it)
 - #77886 (Replace trivial bool matches with the `matches!` macro)
 - #77892 (Replace absolute paths with relative ones)
 - #77895 (Include aarch64-apple-darwin in the dist manifests)
 - #77909 (bootstrap: set correct path for the build-manifest binary)

Failed merges:

 - #77902 (Include aarch64-pc-windows-msvc in the dist manifests)

r? `@ghost`
2020-10-13 22:13:09 +00:00
Ding Xiang Fei f9ccd39ae3
documentation fix 2020-10-14 00:50:54 +08:00
est31 a0fc455d30 Replace absolute paths with relative ones
Modern compilers allow reaching external crates
like std or core via relative paths in modules
outside of lib.rs and main.rs.
2020-10-13 14:16:45 +02:00
Jonas Schievink 432535da2b Refactor how SwitchInt stores jump targets 2020-10-10 17:46:11 +02:00
Yuki Okushi 5c1e01196d
Rollup merge of #77560 - rschoon:fix-litkind-rc-bytebuf, r=lcnr
Fix LitKind's byte buffer to use refcounted slice

While working on adding a new lint for clippy (see https://github.com/rust-lang/rust-clippy/pull/6044) for avoiding shared ownership of "mutable buffer" types (such as using `Rc<Vec<T>>` instead of `Rc<[T]>`), I noticed a type exported from rustc_ast and used by clippy gets caught by the lint. This PR fixes the exported type.

This PR includes the actual change to clippy too, but I will open a PR directly against clippy for that part (although it will currently fail to build there).
2020-10-06 16:26:11 +09:00
bors 62bfcfd8a3 Auto merge of #77552 - ecstatic-morse:body-def-id, r=lcnr
Replace `(Body, DefId)` with `Body` where possible

Follow-up to #77430.

I `grep`-ed for parameter lists in which a `Body` appeared within a few lines of a `DefId`, so it's possible that I missed some cases, but this should be pretty complete. Most of these changes were mechanical, but there's a few places where I started calling things "caller" and "callee" when multiple `DefId`s were in-scope at once. Also, we should probably have a helper function on `Body` that returns a `LocalDefId`. I can do that in this PR or in a follow-up.
2020-10-05 09:26:32 +00:00
bors ced813fec0 Auto merge of #77466 - Aaron1011:reland-drop-tree, r=matthewjasper
Re-land PR #71840 (Rework MIR drop tree lowering)

PR https://github.com/rust-lang/rust/pull/71840 was reverted in https://github.com/rust-lang/rust/pull/72989 to fix an LLVM error (https://github.com/rust-lang/rust/issues/72470). That LLVM error no longer occurs with the recent upgrade to LLVM 11 (https://github.com/rust-lang/rust/pull/73526), so let's try re-landing this PR.

I've cherry-picked the commits from the original PR (with the exception of the commit blessing test output), making as few modifications as possible. I addressed the rebase fallout in separate commits on top of those.

r? `@matthewjasper`
2020-10-05 00:35:58 +00:00
Dylan MacKenzie e72e43c730 Replace `(Body, DefId)` with `Body` where possible
A `Body` now contains its `MirSource`, which in turn contains the
`DefId` of the item associated with the `Body`.
2020-10-04 16:07:03 -07:00
Robin Schoonover 5ab19676ed Remove extra indirection in LitKind::ByteStr 2020-10-04 15:52:15 -06:00
Dylan MacKenzie 606655edc4 HACK: Overwrite the MIR's source with the correct const param
There's a cleaner way of doing this, but it involves passing
`WithOptConstParam` around in more places. We're going to try to explore
different approaches before committing to that.
2020-10-04 11:02:16 -07:00
Dylan MacKenzie 6f61e71648 Remember the `MirSource` for each `Body` 2020-10-04 11:01:38 -07:00
Aaron Hill eb94cdd85e
Apply suggestions from review
Co-authored-by: matthewjasper <20113453+matthewjasper@users.noreply.github.com>
2020-10-04 07:54:03 -04:00
Aaron Hill 8478385ea4
Fix broken link 2020-10-04 07:54:02 -04:00
Aaron Hill 4c83eec008
Fix rebase fallout 2020-10-04 07:54:02 -04:00
Matthew Jasper 8902ce5d84
Address review comments 2020-10-04 07:54:02 -04:00
Matthew Jasper 1e71862046
Add some more comments 2020-10-04 07:54:02 -04:00
Matthew Jasper fa3e2fcbe4
Defer creating drop trees in MIR lowering until leaving that scope 2020-10-04 07:54:01 -04:00
Dylan MacKenzie bb6c249f99 Speed up `IntRange::from_pat`
Previously, this method called the more general `pat_constructor`
function, which can return other pattern variants besides `IntRange`.
Then it throws away any non-`IntRange` variants. Specialize it so work
is only done when it could result in an `IntRange`.
2020-09-26 20:00:54 -07:00
bors fd15e6180d Auto merge of #70743 - oli-obk:eager_const_to_pat_conversion, r=eddyb
Fully destructure constants into patterns

r? `@varkor`

as discussed in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/constants.20in.20patterns/near/192789924

we should probably crater it once reviewed
2020-09-26 06:44:28 +00:00
bors e599b53e67 Auto merge of #76918 - ishitatsuyuki:match-fastpath, r=oli-obk
Add fast path for match checking

This adds a fast path that would reduce the complexity to linear on matches consisting of only variant patterns (i.e. enum matches). (Also see: #7462) Unfortunately, I was too lazy to add a similar fast path for constants (mostly for integer matches), ideally that could be added another day.

TBH, I'm not confident with the performance claims due to the fact that enums tends to be small and FxHashMap could add a lot of overhead.

r? `@Mark-Simulacrum`

needs perf
2020-09-24 17:22:56 +00:00
Oliver Scherer fc9f2947da Document `FallbackToConstRef` and make sure we don't accidentally use it 2020-09-24 17:01:03 +02:00
Oliver Scherer 9550ca6242 Deduplicate the "needs partialeq derive" message creation sites 2020-09-24 10:18:51 +02:00
Oliver Scherer e4928d77a1 Use correct type in diagnostics again 2020-09-24 10:06:07 +02:00
Oliver Scherer da217644a1 Make sure we keep emitting a hard error 2020-09-23 18:36:53 +02:00
Oliver Scherer 017423179a Make sure we report a future incompat error in all cases 2020-09-23 18:04:44 +02:00
Oliver Scherer 6a33de0170 Name function correctly 2020-09-23 17:55:14 +02:00
Oliver Scherer 9a7e66aeaf Make sure we don't hide errors just because a lint has been emitted 2020-09-23 17:52:37 +02:00
Oliver Scherer 177d0cef48 Deduplicate errors in const to pat conversion 2020-09-23 17:03:31 +02:00
Oliver Scherer d486486afd Talk about unpredictable instead of "not deterministic" 2020-09-23 16:38:30 +02:00
Oliver Scherer 1b1b6eabaa Remove the "lift constant to reference" logic 2020-09-23 16:28:45 +02:00
Ishi Tatsuyuki 01a771a7d8 Add debug assertions against slow path reference results 2020-09-22 14:40:44 +09:00
Ishi Tatsuyuki f95e4f3ca9 Improve code and documentation clarity 2020-09-21 20:29:12 +09:00
yuk1ty 16047d46a1 fix typo in docs and comments 2020-09-21 12:14:28 +09:00
Oliver Scherer adf98ab2dc Use precise errors during const to pat conversion instead of a catch-all on the main constant 2020-09-20 18:42:15 +02:00
Oliver Scherer aba5ea1430 Lint on function pointers used in patterns 2020-09-20 18:42:15 +02:00
Oliver Scherer 3795886f7e Split check for `PartialEq` impl into a method 2020-09-20 16:59:15 +02:00
Ralf Jung 8405d50e12
Rollup merge of #76890 - matthiaskrgr:matches_simpl, r=lcnr
use matches!() macro for simple if let conditions
2020-09-20 15:52:01 +02:00
Oliver Scherer b2532a8730 Implement destructuring for all aggregates and for references 2020-09-20 13:28:18 +02:00
Oliver Scherer b54f122a1c Merge tuple and struct pattern generation. 2020-09-20 12:31:37 +02:00
Bastian Kauschke bfb221b21e array pattern 2020-09-20 08:11:05 +02:00
Bastian Kauschke 3435683fd5 use `array_windows` instead of `windows` in the compiler 2020-09-20 08:11:05 +02:00
ishitatsuyuki 7c98f6f584 Add fast path for match checking 2020-09-19 22:00:10 +09:00
Ralf Jung 4831523ac4
Rollup merge of #76757 - matthiaskrgr:clippy_try_into, r=lcnr
don't convert types to the same type with try_into (clippy::useless_conversion)
2020-09-19 11:47:52 +02:00
Matthias Krüger 40dddd3305 use matches!() macro for simple if let conditions 2020-09-18 20:28:35 +02:00
Matthias Krüger f567287f9f don't convert types to the same type with try_into (clippy::useless_conversion) 2020-09-15 22:49:50 +02:00
Amjad Alsharafi da700cba08 Stabilize move_ref_pattern 2020-09-15 14:23:05 +08:00
Aaron Hill d18b4bb7a7
Note when a a move/borrow error is caused by a deref coercion
Fixes #73268

When a deref coercion occurs, we may end up with a move error if the
base value has been partially moved out of. However, we do not indicate
anywhere that a deref coercion is occuring, resulting in an error
message with a confusing span.

This PR adds an explicit note to move errors when a deref coercion is
involved. We mention the name of the type that the deref-coercion
resolved to, as well as the `Deref::Target` associated type being used.
2020-09-10 20:56:20 -04:00
bors 88197214b8 Auto merge of #75573 - Aaron1011:feature/const-mutation-lint, r=oli-obk
Add CONST_ITEM_MUTATION lint

Fixes #74053
Fixes #55721

This PR adds a new lint `CONST_ITEM_MUTATION`.
Given an item `const FOO: SomeType = ..`, this lint fires on:

* Attempting to write directly to a field (`FOO.field = some_val`) or
  array entry (`FOO.array_field[0] = val`)
* Taking a mutable reference to the `const` item (`&mut FOO`), including
  through an autoderef `FOO.some_mut_self_method()`

The lint message explains that since each use of a constant creates a
new temporary, the original `const` item will not be modified.
2020-09-10 05:54:26 +00:00
Aaron Hill f422ef141a
Add CONST_ITEM_MUTATION lint
Fixes #74053
Fixes #55721

This PR adds a new lint `CONST_ITEM_MUTATION`.
Given an item `const FOO: SomeType = ..`, this lint fires on:

* Attempting to write directly to a field (`FOO.field = some_val`) or
  array entry (`FOO.array_field[0] = val`)
* Taking a mutable reference to the `const` item (`&mut FOO`), including
  through an autoderef `FOO.some_mut_self_method()`

The lint message explains that since each use of a constant creates a
new temporary, the original `const` item will not be modified.
2020-09-07 08:44:35 -04:00
Dylan DPC acd33e1d14
Rollup merge of #76318 - scottmcm:one-control-flow, r=ecstatic-morse
Use ops::ControlFlow in rustc_data_structures::graph::iterate

Since I only know about this because you mentioned it,
r? @ecstatic-morse

If we're not supposed to use new `core` things in compiler for a while then feel free to close, but it felt reasonable to merge the two types since they're the same, and it might be convenient for people to use `?` in their traversal code.

(This doesn't do the type parameter swap; NoraCodes has signed up to do that one.)
2020-09-07 01:18:05 +02:00
LeSeulArtichaut 3e14b684dd Change ty.kind to a method 2020-09-04 17:47:51 +02:00
Scott McMurray fac272688e Use ops::ControlFlow in graph::iterate 2020-09-04 01:45:10 -07:00
Dan Aloni 07e7823c01 pretty: trim paths of unique symbols
If a symbol name can only be imported from one place for a type, and
as long as it was not glob-imported anywhere in the current crate, we
can trim its printed path and print only the name.

This has wide implications on error messages with types, for example,
shortening `std::vec::Vec` to just `Vec`, as long as there is no other
`Vec` importable anywhere.

This adds a new '-Z trim-diagnostic-paths=false' option to control this
feature.

On the good path, with no diagnosis printed, we should try to avoid
issuing this query, so we need to prevent trimmed_def_paths query on
several cases.

This change also relies on a previous commit that differentiates
between `Debug` and `Display` on various rustc types, where the latter
is trimmed and presented to the user and the former is not.
2020-09-02 22:26:37 +03:00
mark 9e5f7d5631 mv compiler to compiler/ 2020-08-30 18:45:07 +03:00