Commit graph

269 commits

Author SHA1 Message Date
Esteban Küber bd8b46800d Tweak wording of type errors involving type params
Fix #78206.
2023-10-18 23:53:18 +00:00
Ali MJ Al-Nasrawy a1e274f172 revert rust-lang/rust#114586 2023-10-18 06:19:04 +00:00
Guillaume Gomez d0ade3f1ba
Rollup merge of #116800 - compiler-errors:rpitit-gat-outlives, r=jackh726
Fix implied outlives check for GAT in RPITIT

We enforce certain `Self: 'lt` bounds for GATs to save space for more sophisticated implied bounds, but those currently operate on the HIR. Code was easily reworked to operate on def-ids so that we can properly let these suggestions propagate through synthetic associated types like RPITITs and AFITs.

r? `@jackh726` or `@aliemjay`

Fixes #116789
2023-10-16 23:58:04 +02:00
Michael Goulet 743e6d1601 Remove DefiningAnchor::Bubble from opaque wf check 2023-10-16 15:50:31 +00:00
Michael Goulet 17ec3cd5bf Fix outlives suggestion for GAT in RPITIT 2023-10-16 15:42:26 +00:00
Matthias Krüger 77b578f72b
Rollup merge of #116730 - compiler-errors:unsoundness-tests-rpit, r=aliemjay
Add some unsoundness tests for opaques capturing hidden regions not in substs

Commit tests from https://github.com/rust-lang/rust/pull/116040#issuecomment-1751610237 and https://github.com/rust-lang/rust/pull/59402#issuecomment-476003242 so that we make sure not to regress them the next time that we relax the opaque capture rules :^)
2023-10-14 19:22:18 +02:00
Michael Goulet 3a0799d6d0 Add some unsoundness tests for opaques capturing hidden regions not in substs 2023-10-14 13:26:30 +00:00
Matthias Krüger 45bcef3cd5
Rollup merge of #116689 - lcnr:auto-trait-hidden-ty-leak, r=compiler-errors
explicitly handle auto trait leakage in coherence

does not impact behavior but may avoid weird bugs in the future, cc https://github.com/rust-lang/trait-system-refactor-initiative/issues/65

r? ``@compiler-errors``
2023-10-14 13:48:20 +02:00
Michael Goulet 3f2574e8ba Test that RPITITs have RPIT scope and not impl-wide scope 2023-10-13 21:01:36 +00:00
Michael Goulet 59315b8a63 Stabilize AFIT and RPITIT 2023-10-13 21:01:36 +00:00
lcnr 1bc6ae4401 explicitly handle auto trait leakage in coherence 2023-10-13 09:42:51 +00:00
Matthias Krüger 8ddc0df1f1
Rollup merge of #116219 - compiler-errors:relate-alias-ty-with-variance, r=lcnr
Relate alias ty with variance

In the new solver, turns out that the subst-relate branch of the alias-relate predicate was relating args invariantly even for opaques, which have variance 💀.

This change is a bit more invasive, but I'd rather not special-case it [here](aeaa5c30e5/compiler/rustc_trait_selection/src/solve/alias_relate.rs (L171-L190)) and then have it break elsewhere. I'm doing a perf run to see if the extra call to `def_kind` is that expensive, if it is, I'll reconsider.

r? ``@lcnr``
2023-10-11 20:08:20 +02:00
Ali MJ Al-Nasrawy a8830631b9 remove trailing dots 2023-10-08 10:06:17 +00:00
Ali MJ Al-Nasrawy 996ffcb718 always show and explain sub region 2023-10-08 09:59:51 +00:00
bors 94bc9c737e Auto merge of #114811 - estebank:impl-ambiguity, r=wesleywiser
Show more information when multiple `impl`s apply

- When there are `impl`s without type params, show only those (to avoid showing overly generic `impl`s).
```
error[E0283]: type annotations needed
  --> $DIR/multiple-impl-apply.rs:34:9
   |
LL |     let y = x.into();
   |         ^     ---- type must be known at this point
   |
note: multiple `impl`s satisfying `_: From<Baz>` found
  --> $DIR/multiple-impl-apply.rs:14:1
   |
LL | impl From<Baz> for Bar {
   | ^^^^^^^^^^^^^^^^^^^^^^
...
LL | impl From<Baz> for Foo {
   | ^^^^^^^^^^^^^^^^^^^^^^
   = note: required for `Baz` to implement `Into<_>`
help: consider giving `y` an explicit type
   |
LL |     let y: /* Type */ = x.into();
   |          ++++++++++++
```

- Lower the importance of `T: Sized`, `T: WellFormed` and coercion errors, to prioritize more relevant errors. The pre-existing deduplication logic deals with hiding redundant errors better that way, and we show errors with more metadata that is useful to the user.

- Show `<SelfTy as Trait>::assoc_fn` suggestion in more cases.
```
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
  --> $DIR/cross-return-site-inference.rs:38:16
   |
LL |     return Err(From::from("foo"));
   |                ^^^^^^^^^^ cannot call associated function of trait
   |
help: use a fully-qualified path to a specific available implementation
   |
LL |     return Err(</* self type */ as From>::from("foo"));
   |                +++++++++++++++++++     +
```

Fix #88284.
2023-10-06 18:44:32 +00:00
Matthias Krüger c1c5ab717e
Rollup merge of #116428 - Alexendoo:note-duplicate-diagnostics, r=compiler-errors,estebank
Add a note to duplicate diagnostics

Helps explain why there may be a difference between manual testing and the test suite output and highlights them as something to potentially look into

For existing duplicate diagnostics I just blessed them other than a few files that had other `NOTE` annotations in
2023-10-05 19:24:35 +02:00
ouz-a 3088c4b046 move subtyper change reveal_all 2023-10-05 18:56:30 +03:00
Jubilee d7b02c3d40
Rollup merge of #116431 - estebank:issue-80476, r=compiler-errors
Tweak wording of E0562

Fix #80476.
2023-10-05 00:56:30 -07:00
Jubilee cfce3a919d
Rollup merge of #116296 - compiler-errors:default-return, r=estebank
More accurately point to where default return type should go

When getting the "default return type" span, instead of pointing to the low span of the next token, point to the high span of the previous token. This:

1. Makes forming return type suggestions more uniform, since we expect them all in the same place.
2. Arguably makes labels easier to understand, since we're pointing to where the implicit `-> ()` would've gone, rather than the starting brace or the semicolon.

r? ```@estebank```
2023-10-05 00:56:29 -07:00
bors 5236c8e1fa Auto merge of #116273 - compiler-errors:refine2, r=tmandry
Only trigger `refining_impl_trait` lint on reachable traits

Public but unreachable traits don't matter 😸

r? `@tmandry`
2023-10-05 03:00:30 +00:00
bors b781645332 Auto merge of #116184 - compiler-errors:afit-lint, r=tmandry
Add `async_fn_in_trait` lint

cc https://github.com/rust-lang/rust/pull/115822#issuecomment-1731168465

Mostly unsure what the messaging should be. Feedback required.

r? `@tmandry`
2023-10-05 01:14:25 +00:00
Alex Macleod 5453a9f34d Add a note to duplicate diagnostics 2023-10-05 01:04:41 +00:00
Michael Goulet 137b6d0b01 Point to where missing return type should go 2023-10-04 21:09:54 +00:00
Esteban Küber 041e54bd92 Tweak wording of E0562
Fix #80476.
2023-10-04 19:51:43 +00:00
Michael Goulet 5087bb1046 Relate AliasTy considering variance 2023-10-04 04:22:04 +00:00
Esteban Küber 7313c10774 Show suggestion for <SelfTy as Trait>::assoc_fn in more cases and fmt code 2023-10-04 02:04:14 +00:00
Esteban Küber 91b9ffeab0 Reorder fullfillment errors to keep more interesting ones first
In `report_fullfillment_errors` push back `T: Sized`, `T: WellFormed`
and coercion errors to the end of the list. The pre-existing
deduplication logic eliminates redundant errors better that way, keeping
the resulting output with fewer errors than before, while also having
more detail.
2023-10-04 02:04:14 +00:00
Michael Goulet 28d58f6524 Bless tests 2023-10-03 00:37:18 +00:00
Michael Goulet 07851679cd Point out the actual mismatch error 2023-10-02 23:14:29 +00:00
Michael Goulet 8be12f4ed7 For a single impl candidate, try to unify it with error trait ref 2023-10-02 23:14:29 +00:00
Michael Goulet 06d9602d33 Only trigger refine lint on reachable traits 2023-09-29 18:36:41 +00:00
bors 7b4d9e155f Auto merge of #115659 - compiler-errors:itp, r=cjgillot
Stabilize `impl_trait_projections`

Closes #115659

## TL;DR:

This allows us to mention `Self` and `T::Assoc` in async fn and return-position `impl Trait`, as you would expect you'd be able to.

Some examples:
```rust
#![feature(return_position_impl_trait_in_trait, async_fn_in_trait)]
// (just needed for final tests below)

// ---------------------------------------- //

struct Wrapper<'a, T>(&'a T);

impl Wrapper<'_, ()> {
    async fn async_fn() -> Self {
        //^ Previously rejected because it returns `-> Self`, not `-> Wrapper<'_, ()>`.
        Wrapper(&())
    }

    fn impl_trait() -> impl Iterator<Item = Self> {
        //^ Previously rejected because it mentions `Self`, not `Wrapper<'_, ()>`.
        std::iter::once(Wrapper(&()))
    }
}

// ---------------------------------------- //

trait Trait<'a> {
    type Assoc;
    fn new() -> Self::Assoc;
}
impl Trait<'_> for () {
    type Assoc = ();
    fn new() {}
}

impl<'a, T: Trait<'a>> Wrapper<'a, T> {
    async fn mk_assoc() -> T::Assoc {
        //^ Previously rejected because `T::Assoc` doesn't mention `'a` in the HIR,
        //  but ends up resolving to `<T as Trait<'a>>::Assoc`, which does rely on `'a`.
        // That's the important part -- the elided trait.
        T::new()
    }

    fn a_few_assocs() -> impl Iterator<Item = T::Assoc> {
        //^ Previously rejected for the same reason
        [T::new(), T::new(), T::new()].into_iter()
    }
}

// ---------------------------------------- //

trait InTrait {
    async fn async_fn() -> Self;

    fn impl_trait() -> impl Iterator<Item = Self>;
}

impl InTrait for &() {
    async fn async_fn() -> Self { &() }
    //^ Previously rejected just like inherent impls

    fn impl_trait() -> impl Iterator<Item = Self> {
        //^ Previously rejected just like inherent impls
        [&()].into_iter()
    }
}
```

## Technical:

Lifetimes in return-position `impl Trait` (and `async fn`) are duplicated as early-bound generics local to the opaque in order to make sure we are able to substitute any late-bound lifetimes from the function in the opaque's hidden type. (The [dev guide](https://rustc-dev-guide.rust-lang.org/return-position-impl-trait-in-trait.html#aside-opaque-lifetime-duplication) has a small section about why this is necessary -- this was written for RPITITs, but it applies to all RPITs)

Prior to #103491, all of the early-bound lifetimes not local to the opaque were replaced with `'static` to avoid issues where relating opaques caused their *non-captured* lifetimes to be related. This `'static` replacement led to strange and possibly unsound behaviors (https://github.com/rust-lang/rust/issues/61949#issuecomment-508836314) (https://github.com/rust-lang/rust/issues/53613) when referencing the `Self` type alias in an impl or indirectly referencing a lifetime parameter via a projection type (via a `T::Assoc` projection without an explicit trait), since lifetime resolution is performed on the HIR, when neither `T::Assoc`-style projections or `Self` in impls are expanded.

Therefore an error was implemented in #62849 to deny this subtle behavior as a known limitation of the compiler. It was attempted by `@cjgillot` to fix this in #91403, which was subsequently unlanded. Then it was re-attempted to much success (🎉) in #103491, which is where we currently are in the compiler.

The PR above (#103491) fixed this issue technically by *not* replacing the opaque's parent lifetimes with `'static`, but instead using variance to properly track which lifetimes are captured and are not. The PR gated any of the "side-effects" of the PR behind a feature gate (`impl_trait_projections`) presumably to avoid having to involve T-lang or T-types in the PR as well. `@cjgillot` can clarify this if I'm misunderstanding what their intention was with the feature gate.

Since we're not replacing (possibly *invariant*!) lifetimes with `'static` anymore, there are no more soundness concerns here. Therefore, this PR removes the feature gate.

Tests:
* `tests/ui/async-await/feature-self-return-type.rs`
* `tests/ui/impl-trait/feature-self-return-type.rs`
* `tests/ui/async-await/issues/issue-78600.rs`
* `tests/ui/impl-trait/capture-lifetime-not-in-hir.rs`

---

r? cjgillot on the impl (not much, just removing the feature gate)

I'm gonna mark this as FCP for T-lang and T-types.
2023-09-28 21:35:18 +00:00
Matthias Krüger 50417a5457
Rollup merge of #116149 - compiler-errors:anonymize, r=lcnr
Anonymize binders for `refining_impl_trait` check

We're naively using the equality impl for `ty::Clause` in the refinement check, which is okay *except* for binders, which carry some information about where they come from in the AST. Those locations are not gonna be equal between traits and impls, so anonymize those clauses so that this doesn't matter.

Fixes #116135
2023-09-27 10:42:34 +02:00
bors e1636a0939 Auto merge of #116156 - oli-obk:opaque_place_unwrap, r=compiler-errors
Only prevent field projections into opaque types, not types containing opaque types

fixes https://github.com/rust-lang/rust/issues/115778

I did not think that original condition through properly... I'll also need to check the similar check around the other `ProjectionKind::OpaqueCast` creation site (this one is in hir, the other one is in mir), but I'll do that change in another PR that doesn't go into a beta backport.
2023-09-27 00:03:53 +00:00
Michael Goulet 305524d1d6 Anonymize binders for refining_impl_trait check 2023-09-26 18:11:12 +00:00
bors a61f6f3baa Auto merge of #116072 - compiler-errors:rpitit-implied-bounds, r=aliemjay
Use placeholders to prevent using inferred RPITIT types to imply their own well-formedness

The issue here is that we use the same signature to do RPITIT inference as we do to compute implied bounds. To fix this, when gathering the assumed wf types for the method, we replace all of the infer vars (that will be eventually used to infer RPITIT types) with type placeholders, which imply nothing about lifetime bounds.

This solution kind of sucks, but I'm not certain there's another feasible way to fix this. If anyone has a better solution, I'd be glad to hear it.

My naive first solution was, instead of using placeholders, to replace the signature with the RPITIT projections that it originally started out with. But turns out that we can't just use the unnormalized signature of the trait method in `implied_outlives_bounds` since we normalize during WF computation -- that would cause a query cycle in `collect_return_position_impl_trait_in_trait_tys`.

idk who to request review...
r? `@lcnr` or `@aliemjay` i guess.

Fixes #116060
2023-09-26 01:50:12 +00:00
Oli Scherer 17b313fb57 Only prevent field projections into opaque types, not types containing opaque types 2023-09-25 17:41:08 +00:00
Camille GILLOT 211d2ed07b Bless tests. 2023-09-23 13:47:30 +00:00
Camille GILLOT a626caaad9 Revert duplication of tests. 2023-09-23 13:34:07 +00:00
Michael Goulet 8568121996 Need to use hybrid param-env to make sure implication is not circular 2023-09-22 19:04:37 +00:00
Michael Goulet b5a904a9d4 Use placeholders to prevent using inferred RPITIT types to imply their own WF-ness 2023-09-22 18:55:44 +00:00
bors 959b2c703d Auto merge of #115696 - RalfJung:closure-ty-print, r=oli-obk
adjust how closure/generator types are printed

I saw `&[closure@$DIR/issue-20862.rs:2:5]` and I thought it is a slice type, because that's usually what `&[_]` is... it took me a while to realize that this is just a confusing printer and actually there's no slice. Let's use something that cannot be mistaken for a regular type.
2023-09-22 15:19:38 +00:00
bors 8759de0a49 Auto merge of #114776 - fee1-dead-contrib:enable-effects-in-libcore, r=oli-obk
Enable effects for libcore

~~r? `@oli-obk~~`

forgot you are on vacation, oops
2023-09-22 07:00:52 +00:00
Ralf Jung c4ec12f4b7 adjust how closure/generator types and rvalues are printed 2023-09-21 22:20:58 +02:00
lcnr 8eade3aa71 update tests 2023-09-21 08:17:58 +02:00
Deadbeef 2063ebc3ea bless the known-bug tests 2023-09-20 03:02:14 +00:00
Deadbeef 04eec37dc2 Enable effects for libcore 2023-09-20 03:02:14 +00:00
Oli Scherer 0277184977 Paper over an accidental regression 2023-09-14 15:16:48 +00:00
bors e2b3676733 Auto merge of #114586 - oli-obk:patch_tait_rpit_order_check, r=lcnr,compiler-errors
Bubble up opaque <eq> opaque operations instead of picking an order

In case we are in `Bubble` mode (meaning every opaque type that is defined in the current crate is treated as if it were in its defining scope), we don't try to register an opaque type as the hidden type of another opaque type, but instead bubble up an obligation to equate them at the query caller site. Usually that means we have a `DefiningAnchor::Bind` and thus can reliably figure out whether an opaque type is in its defining scope. Where we can't, we'll error out, so the default is sound.

With this change we start using `AliasTyEq` predicates in the old solver, too.

fixes https://github.com/rust-lang/rust/issues/108498

But also regresses `tests/ui/impl-trait/anon_scope_creep.rs`. Our use of `Bubble` for `check_opaque_type_well_formed` is going to keep biting us.

r? `@lcnr` `@compiler-errors`
2023-09-11 19:01:38 +00:00
Oli Scherer 930affa39d Bubble up opaque <eq> opaque operations instead of picking an order 2023-09-11 16:53:39 +00:00