Skip to content

Conversation

@nick96
Copy link
Contributor

@nick96 nick96 commented Jan 1, 2021

I've made a start on fixing #6913 based on the provided work plan, migrating HasSource::source to return an Option. The simple cases are migrated but there are a few that I'm unsure exactly how they should be handled:

  • Logging the processing of functions in AnalysisStatsCmd::run: In verbose mode it includes the path to the module containing the function and the syntax range. I've handled this with an if-let but would it be better to blow up here with expect? I'm not 100% on the code paths but if we're processing a function definition then the source should exist.

I've handled source() in all code paths as None being a valid return value but are there some cases where we should just blow up? Also, all I've done is bubble up the returned Nones, there may be some places where we can recover and still provide something.

Copy link
Contributor

@matklad matklad left a comment

Choose a reason for hiding this comment

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

lgtm overall!

Either::Right(it) => FieldSource::Named(it),
});
Some(field_source)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As a general comment, during similar refactors its better to delegate rather than duplicate: fn source() { Some(source_old()) }.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, I went for duplicating because it made it easier to remove the old code when this is done.

hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source_old(db).value),
hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source_old(db).value),
}
// Note: This throws away items with no source.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an interesting case! In the future(not this PR), we should add hir -> ast function which either uses an existing source, or just renders the hir directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. I've seen reference to using hir for more in a few comments in the codebase.

}

impl<D> ToNav for D
impl<D> TryToNav for D
Copy link
Contributor

Choose a reason for hiding this comment

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

(not this PR): might make sense to remove plain ToNav alltogether

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that looks to be the case. Looking at the other implementations of ToNav whilst they won't panic, the NavigationTarget's name will be the empty string.

let path = vfs.file_path(original_file);
let syntax_range = src.value.syntax().text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
if let Some(src) = f.source(db) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@nick96 nick96 force-pushed the migrate_hassource_source_return_option branch from 6115f27 to 47bffa0 Compare January 2, 2021 05:38
@nick96 nick96 changed the title [WIP] Migrate HasSource::source to return Option Migrate HasSource::source to return Option Jan 2, 2021
@nick96
Copy link
Contributor Author

nick96 commented Jan 2, 2021

Hmm. I'm not sure why cross compilation is failing after changing to use set_detail. I can't repro it locally using:

$ docker run -v $PWD:/ws --workdir=/ws -it rust bash
$ rustup target add powerpc-unknown-linux-gnu x86_64-unknown-linux-musl
$ for target in powerpc-unknown-linux-gnu x86_64-unknown-linux-musl; do cargo check --target=$target --all-targets; done

@Veykril
Copy link
Member

Veykril commented Jan 2, 2021

I think you need to rebase cause master has ConstParams now, so this impl is outdated when bors tries to merge https://github.com/rust-analyzer/rust-analyzer/blob/aa3ce16f2641b7eb562a8eae67738b0ff0c0b7b0/crates/hir/src/has_source.rs#L144-L150. Weird that only cross compilation fails though.

Edit: This isnt bors failing yet, though you will run into this problem nevertheless.

nick96 and others added 17 commits January 2, 2021 21:53
To start migrating HasSource::source to return an Option.
…os were special cased

In rust-lang#6901 some special case handling for proc-macros was introduced to
prevent panicing as they have no AST. Now the new HasSource::source
method is used that returns an option.

Generally this was a pretty trivial change, the only thing of much
interest is that `hir::MacroDef` now implements `TryToNav` not `ToNav`
as this allows us to handle `HasSource::source` now returning an option.
The `LifetimeParam` and `Local` variants use `source()` to find their
range. Now that `source()` returns an `Option` we need to handle the
`None` case.
…tors optional

They use source() which now returns an Option so they need to too.
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
@nick96 nick96 force-pushed the migrate_hassource_source_return_option branch from 47bffa0 to 40cd6cd Compare January 2, 2021 11:11
@nick96
Copy link
Contributor Author

nick96 commented Jan 2, 2021

Thanks @Veykril, that appears to have fixed it! Not sure why the cross compile job was trying to run that code though as it didn't exist on my branch.

@matklad
Copy link
Contributor

matklad commented Jan 3, 2021

bors r+

cross build uses cargo check, so it is the fastest

@bors
Copy link
Contributor

bors bot commented Jan 3, 2021

@bors bors bot merged commit 520b8a5 into rust-lang:master Jan 3, 2021
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.

3 participants