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

handle {self} when removing unused imports #17140

Merged
merged 1 commit into from
May 23, 2024

Conversation

harrysarson
Copy link
Contributor

Fixes #17139

On master

mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner::{self, X}$0;

    fn f() {
        let y = inner::Y();
    }
}

becomes

mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner:self;

    fn f() {
        let y = inner::Y();
    }
}

with this fix it instead becomes


```rs
mod inner {
    pub struct X();
    pub struct Y();
}

mod z {
    use super::inner;

    fn f() {
        let y = inner::Y();
    }
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 25, 2024
@Veykril
Copy link
Member

Veykril commented Apr 26, 2024

That transform is generally not valid, use foo::bar::{self} imports only the type namespace, where as use foo::bar; imports value, type and macro namespace. So transforming like that can actually cause name collisions.

Now there is still a bug here in that we produce malformed syntax but we need to keep the braces.

@@ -376,11 +376,24 @@ impl ast::UseTreeList {
.filter_map(|it| it.into_token().filter(|it| it.kind() == T![,]))
}

pub fn is_just_self(&self) -> bool {
if let Some((single_subtree,)) = self.use_trees().collect_tuple() {
fn path_is_self(path: &ast::Path) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a closure

Copy link
Contributor Author

@harrysarson harrysarson Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked to make this into a closure, and in the process decided to check the number of use_tree children as part of the same check (so now the closure is called has_single_subtree_that_is_not_self)

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 29, 2024
@harrysarson
Copy link
Contributor Author

That transform is generally not valid, use foo::bar::{self} imports only the type namespace, where as use foo::bar; imports value, type and macro namespace. So transforming like that can actually cause name collisions.

Now there is still a bug here in that we produce malformed syntax but we need to keep the braces.

Yep. I have removed the second commit, the first commit (which fixes remove_unnecessary_braces) I think is what we need to fix the malformed syntax and keep the braces.

Before this commit `UseTree::remove_unnecessary_braces` removed the braces
around `{self}` in `use x::y::{self};` but `use x::y::self;` is not valid
rust.
@harrysarson
Copy link
Contributor Author

harrysarson commented Apr 30, 2024

That transform is generally not valid,

The normalize import IDE assist (https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/normalize_import.rs) does transform {self}. I.e. the following unit test would pass if I added it to normalize_import.rs

    #[test]
    fn test_collapses_self() {
        check_assist_variations!("std::fmt::{self}", "std::fmt");
    }

Is that a bug?

@Veykril
Copy link
Member

Veykril commented May 23, 2024

Unless the assist there checks that fmt only refers to a type namespace there I'd consider that a bug

@Veykril
Copy link
Member

Veykril commented May 23, 2024

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented May 23, 2024

📌 Commit a653ddf has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented May 23, 2024

⌛ Testing commit a653ddf with merge e3e22c6...

@bors
Copy link
Contributor

bors commented May 23, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing e3e22c6 to master...

@bors bors merged commit e3e22c6 into rust-lang:master May 23, 2024
11 checks passed
harrysarson added a commit to harrysarson/rust-analyzer that referenced this pull request Jun 26, 2024
It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// tranforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: rust-lang#17140 (comment)
bors added a commit that referenced this pull request Jul 1, 2024
do not normalize `use foo::{self}` to `use foo`

It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// transforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: #17140 (comment)
roife pushed a commit to roife/rust-analyzer that referenced this pull request Jul 10, 2024
It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// tranforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: rust-lang#17140 (comment)
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// tranforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: rust-lang/rust-analyzer#17140 (comment)
lnicola pushed a commit to lnicola/rust that referenced this pull request Jul 11, 2024
do not normalize `use foo::{self}` to `use foo`

It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// transforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: rust-lang/rust-analyzer#17140 (comment)
10takla pushed a commit to 10takla/rust that referenced this pull request Aug 3, 2024
It changes behaviour and can cause collisions. E.g. for the following snippet

```rs
mod foo {

    pub mod bar {}

    pub const bar: i32 = 8;
}

// tranforming the below to `use foo::bar;` causes the error:
//
//   the name `bar` is defined multiple times
use foo::bar::{self};

const bar: u32 = 99;

fn main() {
    let local_bar = bar;
}
```

we still normalize

```rs
use foo::bar;
use foo::bar::{self};
```

to `use foo::bar;` because this cannot cause collisions.

See: rust-lang/rust-analyzer#17140 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

removing unused imports generates invalid use module_name::self
4 participants