-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Strange async use ...;
compiler suggestion
#87613
Comments
This also appears to happen on the latest stable version (1.54). Here's a playground link. |
Rustc suggests inserting the use directive between |
It looks like the first item in the module is used. We already check if the item span is from a macro expansion. The span for main ends up in the root context somehow. |
There was some discussion in Zulip about this. It's probably a bug in rustc but could indicate a bug in Tokio (if the proc macro is producing root context spans for everything). |
This is probably not a bug in Tokio. This is, at least partially, an artifact of how we generate the suggestion here. Consider this code (playground): /* hey
whazzup */ fn main() {
Command::new("git outta here");
} it generates the following suggestion:
That is, it seems like our system for generating the (My current guess is that this was intended to be a way to generate a reasonable looking level of indentation for the use item, as well as a correct line number for it.) ((Update: Arguably its also trying to make an inference that anywhere you could insert an Update: okay, I should have been a little more thorough in my investigation before. The Update 2: Oddly enough, using |
Okay, so I think the heart of the problem is this:
fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> TokenStream {
input.sig.asyncness = None;
... (This explains why my experiments with adding things like
--- a/tokio-macros/src/entry.rs
+++ b/tokio-macros/src/entry.rs
@@ -293,6 +293,11 @@ fn build_config(
}
fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> TokenStream {
+ if let Some(asyncness) = input.sig.asyncness {
+ if let Some(joined_span) = asyncness.span.join(input.sig.fn_token.span) {
+ input.sig.fn_token.span = joined_span;
+ }
+ }
input.sig.asyncness = None;
// If type mismatch occurs, the current rustc points to the last statement. This is a hack for a number of reasons: a. It sounds pretty goofy to associate a span of unbounded length with the token for a keyword like b. The Having said that, the hack does seem to work (on nightly alone, for the reason given above). Given this source code: #[tokio::main]
/* what the
hey */ async fn main() {
Command::new("git");
} here are the results we see:
For comparison, here is the (same as before) result atop stable:
|
Having said all that, I personally think that the real bug here is how the
Update: I misread the existing code. It does use the span of an existing So, the question now is whether we should try to make do with the span that the |
… r=ekuber More robust fallback for `use` suggestion Our old way to suggest where to add `use`s would first look for pre-existing `use`s in the relevant crate/module, and if there are *no* uses, it would fallback on trying to use another item as the basis for the suggestion. But this was fragile, as illustrated in issue rust-lang#87613 This PR instead identifies span of the first token after any inner attributes, and uses *that* as the fallback for the `use` suggestion. Fix rust-lang#87613
Really minor issue, but when building the following, I get a strange fix suggestion which seems incorrect:
The
async use
seems to be a bit strange for the suggestion. Nevertheless, have a wonderful day <3The text was updated successfully, but these errors were encountered: