Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloaded slice operations to the prelude #17711

Closed
wants to merge 1 commit into from

Conversation

aturon
Copy link
Member

@aturon aturon commented Oct 2, 2014

methods on slice types. However, the relevant traits were not included
in the prelude, meaning that you would have to manually import
std::ops::Slice to get these operations -- an unintended regression.

This commit adds the traits to the prelude, as is done with most other
operator traits.

Unfortunately, the trait std::slice::Slice (which defines an
as_slice method) was already included in the prelude. This trait is
renamed to AsSlice, which is a better name in any case.

In addition, because of both AsSlice and Str traits being included
in the prelude, both of which define as_slice, and both of which are
used for generic programming, this commit renames ops::Slice::as_slice
to ops::Slice::as_slice_. This is a stopgap solution; we'll need to
figure out a long-term story here later on.

Closes #17710

Due to the renamings, this is a:

[breaking-change]

@aturon
Copy link
Member Author

aturon commented Oct 2, 2014

r? @nick29581 @alexcrichton

cc @SimonSapin

The new overloaded slice operations have replaced the various `slice`
methods on slice types. However, the relevant traits were not included
in the prelude, meaning that you would have to manually import
`std::ops::Slice` to get these operations -- an unintended regression.

This commit adds the traits to the prelude, as is done with most other
operator traits.

Unfortunately, the trait `std::slice::Slice` (which defines an
`as_slice` method) was already included in the prelude. This trait is
renamed to `AsSlice`, which is a better name in any case.

In addition, because of both `AsSlice` and `Str` traits being included
in the prelude, both of which define `as_slice`, and both of which are
used for generic programming, this commit renames `ops::Slice::as_slice`
to `ops::Slice::as_slice_`. This is a stopgap solution; we'll need to
figure out a long-term story here later on.

Closes rust-lang#17710

Due to the renamings, this is a:

[breaking-change]
@aturon
Copy link
Member Author

aturon commented Oct 2, 2014

A few notes about this PR: ultimately it's not clear whether the Str and AsSlice traits should continue to exist, but it's also not clear whether the [] notation is going to stick around (depending on deref and coercion decisions).

Also, at the moment removing Str and AsSlice in favor of ops::Slice is painful due to staging issues: everywhere we call as_slice we'd have to call as_slice_ instead.

This PR therefore is the smallest delta that deals with the basic regression. After it lands, we'll need to figure out a real solution for these problems.

@aturon
Copy link
Member Author

aturon commented Oct 2, 2014

I've run into several additional problems, so I'm going to close this and instead revert #17620

@aturon aturon closed this Oct 2, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Aug 29, 2024
fix: Wrong BoundVar index when lowering impl trait parameter of parent generics

Fixes rust-lang#17711

From the following test code;

```rust
//- minicore: deref
use core::ops::Deref;

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

trait Trait {}

impl<'a, T: Deref<Target = impl Trait>> Struct<'a, T> {
    fn foo(&self) -> &Self { self }

    fn bar(&self) {
        let _ = self.foo();
    }

}
```

when we call `register_obligations_for_call` for `let _ = self.foo();`,

https://github.com/rust-lang/rust-analyzer/blob/07659783fdfd4ec0a0bffa93017e33e31e567e42/crates/hir-ty/src/infer/expr.rs#L1939-L1952

we are querying `generic_predicates` and it has `T: Deref<Target = impl Trait>` predicate from the parent `impl Struct`;

https://github.com/rust-lang/rust-analyzer/blob/07659783fdfd4ec0a0bffa93017e33e31e567e42/crates/hir-ty/src/lower.rs#L375-L399

but as we can see above, lowering `TypeRef = impl Trait` doesn't take into account the parent generic parameters, so the `BoundVar` index here is `0`, as `fn foo` has no generic args other than parent's,

But this `BoundVar` is pointing at `'a` in `<'a, T: Deref<Target = impl Trait>>`.
So, in the first code reference `register_obligations_for_call`'s L:1948 - `.substitute(Interner, parameters)`, we are substituting `'a` with `Ty`, not `Lifetime` and this makes panic inside the chalk.

This PR fixes this wrong `BoundVar` index in such cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

std::ops::Slice is not in the prelude
1 participant