Auto merge of #14176 - lowr:fix/assoc-func-vis-in-local-impl, r=Veykril

Fix associated item visibility in block-local impls

Fixes #14046

When we're resolving visibility of block-local items...

> `self` normally refers to the containing non-block module, and `super` to its parent (etc.). However, visibilities must only refer to a module in the DefMap they're written in, so we restrict them when that happens. ([link])

 ...unless we're resolving visibility of associated items in block-local impls, because that impl is semantically "hoisted" to the nearest (non-block) module. With this PR, we skip the adjustment for such items.

Since visibility representation of those items is modified, this PR also adjusts visibility rendering in `HirDisplay`.

[link]: a6603fc21d/crates/hir-def/src/nameres/path_resolution.rs (L101-L103)
This commit is contained in:
bors 2023-03-01 12:40:55 +00:00
commit 32424d0aba
10 changed files with 282 additions and 30 deletions

View file

@ -5619,3 +5619,181 @@ fn main() {
"#]],
);
}
#[test]
fn assoc_fn_in_block_local_impl() {
check(
r#"
struct S;
mod m {
const _: () = {
impl crate::S {
pub(crate) fn foo() {}
}
};
}
fn test() {
S::foo$0();
}
"#,
expect![[r#"
*foo*
```rust
test::S
```
```rust
pub(crate) fn foo()
```
"#]],
);
check(
r#"
struct S;
mod m {
const _: () = {
const _: () = {
impl crate::S {
pub(crate) fn foo() {}
}
};
};
}
fn test() {
S::foo$0();
}
"#,
expect![[r#"
*foo*
```rust
test::S
```
```rust
pub(crate) fn foo()
```
"#]],
);
check(
r#"
struct S;
mod m {
mod inner {
const _: () = {
impl crate::S {
pub(super) fn foo() {}
}
};
}
fn test() {
crate::S::foo$0();
}
}
"#,
expect![[r#"
*foo*
```rust
test::S
```
```rust
pub(super) fn foo()
```
"#]],
);
}
#[test]
fn assoc_const_in_block_local_impl() {
check(
r#"
struct S;
mod m {
const _: () = {
impl crate::S {
pub(crate) const A: () = ();
}
};
}
fn test() {
S::A$0;
}
"#,
expect![[r#"
*A*
```rust
test
```
```rust
pub(crate) const A: () = ()
```
"#]],
);
check(
r#"
struct S;
mod m {
const _: () = {
const _: () = {
impl crate::S {
pub(crate) const A: () = ();
}
};
};
}
fn test() {
S::A$0;
}
"#,
expect![[r#"
*A*
```rust
test
```
```rust
pub(crate) const A: () = ()
```
"#]],
);
check(
r#"
struct S;
mod m {
mod inner {
const _: () = {
impl crate::S {
pub(super) const A: () = ();
}
};
}
fn test() {
crate::S::A$0;
}
}
"#,
expect![[r#"
*A*
```rust
test
```
```rust
pub(super) const A: () = ()
```
"#]],
);
}