-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: implement a simple hover auto traits in rpit #23240
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,6 +261,52 @@ pub(super) fn keyword( | |
| Some(HoverResult { markup, actions }) | ||
| } | ||
|
|
||
| pub(super) fn rpit( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is the wrong way to fix. We should instead do that in display infra, for every printed opaque. Granted, it'll need more work because we don't currently carry the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think these are two different features, regarding RPIT and any expressions fn foo() -> Cell<impl Trait> { Cell::new(2) }
//^^^^ impl Trait + Send + Sync + ...
// impl Trait = i32fn foo() -> Cell<impl Trait> { Cell::new(2) }
//^^^^^^^^^^^^ Cell<i32>
// implement auto traits: Send + ...fn foo() -> Cell<impl Trait> { Cell::new(2) }
//^ i32
// implement auto traits: Send + Sync + ...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. I'm not saying to add a clause "implements auto traits" similar to "implements notable traits", only to add this information when displaying opaques.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a feature that is bound to the function return, and the opaque semantics in other places are different, such as in the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a feature that is relevant to any opqaue - RPIT, ATPIT, ....
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm…, On the other hand, the current implementation is simple and very practical (in the stable version) |
||
| sema: &Semantics<'_, RootDatabase>, | ||
| _config: &HoverConfig<'_>, | ||
| token: &SyntaxToken, | ||
| edition: Edition, | ||
| display_target: DisplayTarget, | ||
| ) -> Option<HoverResult> { | ||
| // FIXME: offer on 'async' kw, outputs async fn desugared rpit | ||
| if token.kind() != T![impl] { | ||
| return None; | ||
| } | ||
|
|
||
| let db = sema.db; | ||
|
|
||
| let impl_type = token.parent().and_then(ast::ImplTraitType::cast)?; | ||
| // FIXME: supports nested rpit, like `Option<impl Trait>` #23237 | ||
| let ret_type = impl_type.syntax().parent().and_then(ast::RetType::cast)?; | ||
| let func = ret_type.syntax().parent().and_then(ast::Fn::cast)?; | ||
| // XXX: Should be resolve type and iteratively the bound list, not a string | ||
| let exists = impl_type | ||
| .type_bound_list()? | ||
| .bounds() | ||
| .filter_map(|it| Some(it.ty()?.to_string())) | ||
| .collect_vec(); | ||
|
|
||
| let mut res = impl_type.to_string(); | ||
|
|
||
| let body = func.body()?; | ||
| let ty = sema.type_of_expr(&body.into())?.adjusted(); | ||
|
|
||
| let auto_traits = sema | ||
| .scope(impl_type.syntax())? | ||
| .krate() | ||
| .auto_traits_in_deps(db) | ||
| .map(|trait_| Trait::from(*trait_)) | ||
| .filter(|trait_| trait_.is_auto(db) && ty.impls_trait(db, *trait_, &[])) | ||
| .filter(|trait_| !exists.iter().any(|it| it == trait_.name(db).as_str())); | ||
| for trait_ in auto_traits { | ||
| format_to!(res, " + {}", trait_.name(db).display(db, edition)); | ||
| } | ||
| format_to!(res, "\n{impl_type} = {}", ty.display(db, display_target)); | ||
|
|
||
| let markup = format!("```rust\n{res}\n```").into(); | ||
| Some(HoverResult { markup, ..Default::default() }) | ||
| } | ||
|
|
||
| /// Returns missing types in a record pattern. | ||
| /// Only makes sense when there's a rest pattern in the record pattern. | ||
| /// i.e. `let S {a, ..} = S {a: 1, b: 2}` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,7 +143,7 @@ fn foo<T>() {} | |
| file_id: FileId( | ||
| 1, | ||
| ), | ||
| range: 470..475, | ||
| range: 526..531, | ||
| }, | ||
| ), | ||
| ), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should IMO be the same query with
crate_notable_traits(). Also both should check the presence of the unsafe feature like we do forcrate_lang_items().View changes since the review