Commit graph

394 commits

Author SHA1 Message Date
Lucas Kent 130b9e9e3b Improve diagnostics when a static lifetime is expected 2021-11-14 12:04:39 +11:00
bors 3d29b68077 Auto merge of #90648 - matthewjasper:assoc-item-cleanup, r=cjgillot
Assoc item cleanup

This removes some fields from ObligationCauseCode

Split out of #90639
2021-11-11 15:15:15 +00:00
Matthias Krüger fd74c93403
Rollup merge of #89561 - nbdd0121:const_typeck, r=nikomatsakis
Type inference for inline consts

Fixes #78132
Fixes #78174
Fixes #81857
Fixes #89964

Perform type checking/inference of inline consts in the same context as the outer def, similar to what is currently done to closure.

Doing so would require `closure_base_def_id` of the inline const to return the outer def, and since `closure_base_def_id` can be called on non-local crate (and thus have no HIR available), a new `DefKind` is created for inline consts.

The type of the generated anon const can capture lifetime of outer def, so we couldn't just use the typeck result as the type of the inline const's def. Closure has a similar issue, and it uses extra type params `CK, CS, U` to capture closure kind, input/output signature and upvars. I use a similar approach for inline consts, letting it have an extra type param `R`, and then `typeof(InlineConst<[paremt generics], R>)` would just be `R`. In borrowck region requirements are also propagated to the outer MIR body just like it's currently done for closure.

With this PR, inline consts in expression position are quitely usable now; however the usage in pattern position is still incomplete -- since those does not remain in the MIR borrowck couldn't verify the lifetime there. I have left an ignored test as a FIXME.

Some disucssions can be found on [this Zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/inline.20consts.20typeck).
cc `````@spastorino````` `````@lcnr`````
r? `````@nikomatsakis`````

`````@rustbot````` label A-inference F-inline_const T-compiler
2021-11-09 19:00:40 +01:00
Deadbeef d863021521
fmt 2021-11-08 23:55:51 +08:00
Deadbeef f1126f1272
Make select_* methods return Vec for TraitEngine 2021-11-08 23:35:23 +08:00
Matthias Krüger 5c454551da more clippy fixes 2021-11-07 16:59:05 +01:00
Matthew Jasper 9734c03524 Remove some fields from ObligationCauseCode 2021-11-07 13:20:02 +00:00
Gary Guo c4103d438f Rename functions reflect that inline const is also "typeck_child" 2021-11-07 04:00:34 +00:00
bors 9d39f6ab7d Auto merge of #89970 - jackh726:gats_diagnostics, r=nikomatsakis
Implementation of GATs outlives lint

See #87479 for background. Closes #87479

The basic premise of this lint/error is to require the user to write where clauses on a GAT when those bounds can be implied or proven from any function on the trait returning that GAT.

## Intuitive Explanation (Attempt) ##
Let's take this trait definition as an example:
```rust
trait Iterable {
    type Item<'x>;
    fn iter<'a>(&'a self) -> Self::Item<'a>;
}
```
Let's focus on the `iter` function. The first thing to realize is that we know that `Self: 'a` because of `&'a self`. If an impl wants `Self::Item` to contain any data with references, then those references must be derived from `&'a self`. Thus, they must live only as long as `'a`. Furthermore, because of the `Self: 'a` implied bound, they must live only as long as `Self`. Since it's `'a` is used in place of `'x`, it is reasonable to assume that any value of `Self::Item<'x>`, and thus `'x`, will only be able to live as long as `Self`. Therefore, we require this bound on `Item` in the trait.

As another example:
```rust
trait Deserializer<T> {
    type Out<'x>;
    fn deserialize<'a>(&self, input: &'a T) -> Self::Out<'a>;
}
```
The intuition is similar here, except rather than a `Self: 'a` implied bound, we have a `T: 'a` implied bound. Thus, the data on `Self::Out<'a>` is derived from `&'a T`, and thus it is reasonable to expect that the lifetime `'x` will always be less than `T`.

## Implementation Algorithm ##
* Given a GAT `<P0 as Trait<P1..Pi>>::G<Pi...Pn>` declared as `trait T<A1..Ai> for A0 { type G<Ai...An>; }` used in return type of one associated function `F`
* Given env `E` (including implied bounds) for `F`
* For each lifetime parameter `'a` in `P0...Pn`:
    * For each other type parameter `Pi != 'a` in `P0...Pn`: // FIXME: this include of lifetime parameters too
        * If `E => (P: 'a)`:
            * Require where clause `Ai: 'a`

## Follow-up questions ##
* What should we do when we don't pass params exactly?
For this example:
```rust
trait Des {
    type Out<'x, D>;
    fn des<'z, T>(&self, data: &'z Wrap<T>) -> Self::Out<'z, Wrap<T>>;
}
```
Should we be requiring a `D: 'x` clause? We pass `Wrap<T>` as `D` and `'z` as `'x`, and should be able to prove that `Wrap<T>: 'z`.

r? `@nikomatsakis`
2021-11-06 04:15:22 +00:00
Nilstrieb 4b9e4606cb Add beginner friendly lifetime elision hint to E0623
Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't

Issue #90170

This also changes the tests introduced by the previous commits because of another rustc issue (#90258)
2021-11-03 20:10:44 +01:00
Matthias Krüger 88e0bea7ca
Rollup merge of #90395 - b-naber:const-expr-type-relation, r=oli-obk
Restrict liveness of mutable borrow of inner infcx in ConstInferUnifier::consts

Fixes https://github.com/rust-lang/rust/issues/89304

r? ``@oli-obk``
2021-10-30 14:37:02 +02:00
b-naber 04cb19692c don't mutably borrow inner infcx in all of ConstInferUnifier::consts 2021-10-29 12:32:06 +02:00
bors 88a5a984fe Auto merge of #90380 - Mark-Simulacrum:revert-89558-query-stable-lint, r=lcnr
Revert "Add rustc lint, warning when iterating over hashmaps"

Fixes perf regressions introduced in https://github.com/rust-lang/rust/pull/90235 by temporarily reverting the relevant PR.
2021-10-29 04:55:51 +00:00
Mark Rousskov 3215eeb99f
Revert "Add rustc lint, warning when iterating over hashmaps" 2021-10-28 11:01:42 -04:00
Oli Scherer bc552fc417 Move instantiate_opaque_types to rustc_infer.
It does not depend on anything from rustc_trait_selection anymore.
2021-10-28 14:12:24 +00:00
Oli Scherer a8f06b249b Move some functions into rustc_infer.
They don't depend on trait selection anymore, so there is no need for an extension trait.
2021-10-28 13:54:11 +00:00
Matthias Krüger 87822b27ee
Rollup merge of #89558 - lcnr:query-stable-lint, r=estebank
Add rustc lint, warning when iterating over hashmaps

r? rust-lang/wg-incr-comp
2021-10-24 15:48:42 +02:00
bors aa5740c715 Auto merge of #90104 - spastorino:coherence-for-negative-trait, r=nikomatsakis
Implement coherence checks for negative trait impls

The main purpose of this PR is to be able to [move Error trait to core](https://github.com/rust-lang/project-error-handling/issues/3).

This feature is necessary to handle the following from impl on box.

```rust
impl From<&str> for Box<dyn Error> { ... }
```

Without having negative traits affect coherence moving the error trait into `core` and moving that `From` impl to `alloc` will cause the from impl to no longer compiler because of a potential future incompatibility. The compiler indicates that `&str` _could_ introduce an `Error` impl in the future, and thus prevents the `From` impl in `alloc` that would cause overlap with `From<E: Error> for Box<dyn Error>`. Adding `impl !Error for &str {}` with the negative trait coherence feature will disable this error by encoding a stability guarantee that `&str` will never implement `Error`, making the `From` impl compile.

We would have this in `alloc`:

```rust
impl From<&str> for Box<dyn Error> {} // A
impl<E> From<E> for Box<dyn Error> where E: Error {} // B
```

and this in `core`:

```rust
trait Error {}
impl !Error for &str {}
```

r? `@nikomatsakis`

This PR was built on top of `@yaahc` PR #85764.

Language team proposal: to https://github.com/rust-lang/lang-team/issues/96
2021-10-23 12:51:15 +00:00
Michael Howell 98ed5548d7 nice_region_error: Include lifetime placeholders in error output
As you can see in src/test/ui/traits/self-without-lifetime-constraint.stderr
you can get very confusing type names if you don't have this.

Fixes #87763
2021-10-22 15:26:20 -07:00
Santiago Pastorino c4c76a4fbd
Document flip polarity 2021-10-22 09:34:36 -03:00
Yuki Okushi afdd0c3ade
Rollup merge of #90071 - cjgillot:no-blocks, r=oli-obk
Remove hir::map::blocks and use FnKind instead

The principal tool is `FnLikeNode`, which is not often used and can be easily implemented using `rustc_hir::intravisit::FnKind`.
2021-10-21 14:11:08 +09:00
Santiago Pastorino 68d444ffa1
Add TraitObligation::polarity() for better encapsulation 2021-10-20 14:45:10 -03:00
Santiago Pastorino 8b0bfb0dcb
Consider negative polarity on overlap check 2021-10-20 12:10:45 -03:00
Camille GILLOT 6e98688e68 Replace FnLikeNode by FnKind. 2021-10-19 23:31:51 +02:00
bors 1af55d19c7 Auto merge of #89933 - est31:let_else, r=michaelwoerister
Adopt let_else across the compiler

This performs a substitution of code following the pattern:

```
let <id> = if let <pat> = ... { identity } else { ... : ! };
```

To simplify it to:

```
let <pat> = ... { identity } else { ... : ! };
```

By adopting the `let_else` feature (cc #87335).

The PR also updates the syn crate because the currently used version of the crate doesn't support `let_else` syntax yet.

Note: Generally I'm the person who *removes* usages of unstable features from the compiler, not adds more usages of them, but in this instance I think it hopefully helps the feature get stabilized sooner and in a better state. I have written a [comment](https://github.com/rust-lang/rust/issues/87335#issuecomment-944846205) on the tracking issue about my experience and what I feel could be improved before stabilization of `let_else`.
2021-10-19 14:41:39 +00:00
bors ec724ac075 Auto merge of #89229 - oli-obk:i_love_inferctxt, r=jackh726
Remove redundant member-constraint check

impl trait will, for each lifetime in the hidden type, register a "member constraint" that says the lifetime must be equal or outlive one of the lifetimes of the impl trait. These member constraints will be solved by borrowck

But, as you can see in the big red block of removed code, there was an ad-hoc check for member constraints happening at the site where they get registered. This check had some minor effects on diagnostics, but will fall down on its feet with my big type alias impl trait refactor. So we removed it and I pulled the removal out into a (hopefully) reviewable PR that works on master directly.
2021-10-18 23:02:53 +00:00
Oli Scherer 4413f8c709 Member constraints already covered all of E0482 already, so that error never occurred anymore 2021-10-18 15:50:56 +00:00
Oli Scherer 2431540b62 Remove unused enum variant 2021-10-18 15:50:56 +00:00
Oli Scherer b02f2982e7 Remove regionck member constraint handling and leave it to mir borrowck 2021-10-18 15:50:56 +00:00
jackh726 82148cdc66 Change outlives clause checking algorithm 2021-10-16 19:04:39 -04:00
Matthias Krüger 502d57b32d
Rollup merge of #89915 - jackh726:outlives_cleanup, r=nikomatsakis
Some outlives cleanup

No semantic changes here, only moving code around + using `LocalDefId` instead of `HirId`

r? ````@nikomatsakis````
2021-10-16 08:02:25 +02:00
est31 1418df5888 Adopt let_else across the compiler
This performs a substitution of code following the pattern:

let <id> = if let <pat> = ... { identity } else { ... : ! };

To simplify it to:

let <pat> = ... { identity } else { ... : ! };

By adopting the let_else feature.
2021-10-16 07:18:05 +02:00
Oli Scherer 38b9e6a393 Document the new logic 2021-10-15 17:50:33 +00:00
Oli Scherer 3ea956b8c5 Remove a now-unused trait 2021-10-15 17:50:33 +00:00
Oli Scherer 07b8bbb1f6 Equality of regions is not just on identity, but if both regions outlive each other 2021-10-15 17:50:33 +00:00
Oli Scherer 6f71cab3b0 Normalize regions before comparing them for member constraints 2021-10-15 17:50:33 +00:00
Oli Scherer 5115069ccd Add some more instrumentation 2021-10-15 17:50:33 +00:00
jackh726 a7c132de55 Move push_outlives_components to rustc_infer 2021-10-15 12:13:35 -04:00
jackh726 c4f9eb1e5a Emit impl difference error for GenericBoundFailure too 2021-10-15 11:41:39 -04:00
lcnr 00e5abe9b6 allow potential_query_instability everywhere 2021-10-15 10:58:18 +02:00
Oli Scherer 888ba509ea Re-use logic for adding a suggestion when a lifetime bound is missing on an impl trait 2021-10-13 10:53:44 +00:00
Oli Scherer d435101537 Use a label instead of a note for member constraint errors 2021-10-13 10:53:44 +00:00
Oli Scherer 15f93473f1 Remove textual span from diagnostic string 2021-10-13 10:53:44 +00:00
bors 68dfa07e3b Auto merge of #89633 - rhysd:issue-65230, r=petrochenkov
Show detailed expected/found types in error message when trait paths are the same

Fixes #65230.

### Issue solved by this PR

```rust
trait T {
    type U;
    fn f(&self) -> Self::U;
}

struct X<'a>(&'a mut i32);

impl<'a> T for X<'a> {
    type U = &'a i32;
    fn f(&self) -> Self::U {
        self.0
    }
}

fn main() {}
```

Compiler generates the following note:

```
note: ...so that the types are compatible
  --> test.rs:10:28
   |
10 |       fn f(&self) -> Self::U {
   |  ____________________________^
11 | |         self.0
12 | |     }
   | |_____^
   = note: expected `T`
              found `T`
```

This note is not useful since the expected type and the found type are the same.

### How this PR solve the issue

When the expected type and the found type are exactly the same in string representation, the note falls back to the detailed string representation of trait ref:

```
note: ...so that the types are compatible
  --> test.rs:10:28
   |
10 |       fn f(&self) -> Self::U {
   |  ____________________________^
11 | |         self.0
12 | |     }
   | |_____^
   = note: expected `<X<'a> as T>`
              found `<X<'_> as T>`
```

So that a user can notice what was different between the expected one and the found one.
2021-10-10 15:59:28 +00:00
Matthias Krüger e6f77a1787 clippy::complexity fixes 2021-10-08 20:07:44 +02:00
rhysd 7b9ddbdcf2 Show detailed expected/found types in error message when trait paths are the same 2021-10-09 00:07:37 +09:00
bors ca8078d7b2 Auto merge of #89495 - Mark-Simulacrum:add-inlines, r=michaelwoerister
Add two inline annotations for hot functions

These two functions are essentially no-ops (and compile to just a load and
return), but show up in process_obligations profiles with a high call count --
so worthwhile to try and inline them. This is not normally possible as they're
non-generic, so they don't get offered for inlining by our current algorithm.
2021-10-07 06:23:23 +00:00
Mark Rousskov 1c2ad79f9d Add two inline annotations for hot functions
These two functions are essentially no-ops (and compile to just a load and
return), but show up in process_obligations profiles with a high call count --
so worthwhile to try and inline them away.
2021-10-03 12:43:43 -04:00
Cameron Steffen eec856bfbc Make diangostic item names consistent 2021-10-02 19:38:19 -05:00
Oli Scherer 9b5aa063d8 More tracing instrumentation 2021-09-28 12:28:22 +00:00