2022-08-31 11:34:10 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-10-20 10:04:38 -05:00
|
|
|
use either::Either;
|
2022-11-04 11:11:15 -05:00
|
|
|
use hir::{
|
|
|
|
known, Adjust, AutoBorrow, Callable, HasVisibility, HirDisplay, Mutability, OverloadedDeref,
|
|
|
|
PointerCast, Safety, Semantics, TypeInfo,
|
|
|
|
};
|
2022-03-19 12:11:56 -05:00
|
|
|
use ide_db::{
|
2022-04-25 11:51:59 -05:00
|
|
|
base_db::FileRange, famous_defs::FamousDefs, syntax_helpers::node_ext::walk_ty, FxHashMap,
|
|
|
|
RootDatabase,
|
2022-03-19 12:11:56 -05:00
|
|
|
};
|
2021-10-03 08:31:35 -05:00
|
|
|
use itertools::Itertools;
|
2020-08-12 11:26:51 -05:00
|
|
|
use stdx::to_lower_snake_case;
|
|
|
|
use syntax::{
|
2022-03-18 12:11:16 -05:00
|
|
|
ast::{self, AstNode, HasArgList, HasGenericParams, HasName, UnaryOp},
|
2022-05-17 06:39:45 -05:00
|
|
|
match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
|
|
|
|
TextSize, T,
|
2019-07-19 16:20:09 -05:00
|
|
|
};
|
2019-12-21 11:45:46 -06:00
|
|
|
|
2020-07-16 14:51:44 -05:00
|
|
|
use crate::FileId;
|
2020-07-16 11:07:53 -05:00
|
|
|
|
2020-03-10 13:21:56 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2020-03-31 09:02:55 -05:00
|
|
|
pub struct InlayHintsConfig {
|
2022-03-11 14:06:26 -06:00
|
|
|
pub render_colons: bool,
|
2020-03-11 22:14:39 -05:00
|
|
|
pub type_hints: bool,
|
|
|
|
pub parameter_hints: bool,
|
2020-03-23 14:32:05 -05:00
|
|
|
pub chaining_hints: bool,
|
2022-11-04 11:11:15 -05:00
|
|
|
pub adjustment_hints: AdjustmentHints,
|
2022-05-28 07:13:25 -05:00
|
|
|
pub closure_return_type_hints: ClosureReturnTypeHints,
|
2022-05-14 07:26:08 -05:00
|
|
|
pub binding_mode_hints: bool,
|
2022-03-19 13:01:19 -05:00
|
|
|
pub lifetime_elision_hints: LifetimeElisionHints,
|
2022-03-19 12:11:56 -05:00
|
|
|
pub param_names_for_lifetime_elision_hints: bool,
|
2021-11-13 17:12:29 -06:00
|
|
|
pub hide_named_constructor_hints: bool,
|
2022-05-15 06:17:52 -05:00
|
|
|
pub hide_closure_initialization_hints: bool,
|
2020-03-10 13:21:56 -05:00
|
|
|
pub max_length: Option<usize>,
|
2022-05-13 12:42:59 -05:00
|
|
|
pub closing_brace_hints_min_lines: Option<usize>,
|
2020-03-10 13:21:56 -05:00
|
|
|
}
|
|
|
|
|
2022-05-28 07:13:25 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum ClosureReturnTypeHints {
|
|
|
|
Always,
|
|
|
|
WithBlock,
|
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:01:19 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum LifetimeElisionHints {
|
|
|
|
Always,
|
|
|
|
SkipTrivial,
|
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2022-05-12 06:39:32 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2022-11-04 11:11:15 -05:00
|
|
|
pub enum AdjustmentHints {
|
2022-05-12 06:39:32 -05:00
|
|
|
Always,
|
2022-11-04 16:59:07 -05:00
|
|
|
ReborrowOnly,
|
2022-05-12 06:39:32 -05:00
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2020-03-10 13:21:56 -05:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2019-07-19 16:20:09 -05:00
|
|
|
pub enum InlayKind {
|
2022-05-14 07:26:08 -05:00
|
|
|
BindingModeHint,
|
2020-03-23 14:32:05 -05:00
|
|
|
ChainingHint,
|
2022-05-17 07:46:43 -05:00
|
|
|
ClosingBraceHint,
|
2022-04-26 04:46:03 -05:00
|
|
|
ClosureReturnTypeHint,
|
2022-03-18 12:11:16 -05:00
|
|
|
GenericParamListHint,
|
2022-11-04 11:11:15 -05:00
|
|
|
AdjustmentHint,
|
2022-03-18 12:11:16 -05:00
|
|
|
LifetimeHint,
|
2022-04-26 04:46:03 -05:00
|
|
|
ParameterHint,
|
|
|
|
TypeHint,
|
2022-12-16 13:52:31 -06:00
|
|
|
OpeningParenthesis,
|
|
|
|
ClosingParenthesis,
|
2019-07-19 16:20:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InlayHint {
|
|
|
|
pub range: TextRange,
|
2019-07-22 13:52:47 -05:00
|
|
|
pub kind: InlayKind,
|
2022-08-31 11:34:10 -05:00
|
|
|
pub label: InlayHintLabel,
|
2022-05-19 06:38:37 -05:00
|
|
|
pub tooltip: Option<InlayTooltip>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum InlayTooltip {
|
|
|
|
String(String),
|
|
|
|
HoverRanged(FileId, TextRange),
|
|
|
|
HoverOffset(FileId, TextSize),
|
2019-07-19 16:20:09 -05:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:34:10 -05:00
|
|
|
pub struct InlayHintLabel {
|
|
|
|
pub parts: Vec<InlayHintLabelPart>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InlayHintLabel {
|
|
|
|
pub fn as_simple_str(&self) -> Option<&str> {
|
|
|
|
match &*self.parts {
|
|
|
|
[part] => part.as_simple_str(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prepend_str(&mut self, s: &str) {
|
|
|
|
match &mut *self.parts {
|
|
|
|
[part, ..] if part.as_simple_str().is_some() => part.text = format!("{s}{}", part.text),
|
|
|
|
_ => self.parts.insert(0, InlayHintLabelPart { text: s.into(), linked_location: None }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn append_str(&mut self, s: &str) {
|
|
|
|
match &mut *self.parts {
|
|
|
|
[.., part] if part.as_simple_str().is_some() => part.text.push_str(s),
|
|
|
|
_ => self.parts.push(InlayHintLabelPart { text: s.into(), linked_location: None }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for InlayHintLabel {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Self { parts: vec![InlayHintLabelPart { text: s, linked_location: None }] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:11:15 -05:00
|
|
|
impl From<&str> for InlayHintLabel {
|
|
|
|
fn from(s: &str) -> Self {
|
|
|
|
Self { parts: vec![InlayHintLabelPart { text: s.into(), linked_location: None }] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:34:10 -05:00
|
|
|
impl fmt::Display for InlayHintLabel {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.parts.iter().map(|part| &part.text).format(""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for InlayHintLabel {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_list().entries(&self.parts).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InlayHintLabelPart {
|
|
|
|
pub text: String,
|
2022-08-31 11:54:19 -05:00
|
|
|
/// Source location represented by this label part. The client will use this to fetch the part's
|
|
|
|
/// hover tooltip, and Ctrl+Clicking the label part will navigate to the definition the location
|
|
|
|
/// refers to (not necessarily the location itself).
|
|
|
|
/// When setting this, no tooltip must be set on the containing hint, or VS Code will display
|
|
|
|
/// them both.
|
2022-08-31 11:34:10 -05:00
|
|
|
pub linked_location: Option<FileRange>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InlayHintLabelPart {
|
|
|
|
pub fn as_simple_str(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
Self { text, linked_location: None } => Some(text),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for InlayHintLabelPart {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self.as_simple_str() {
|
|
|
|
Some(string) => string.fmt(f),
|
|
|
|
None => f
|
|
|
|
.debug_struct("InlayHintLabelPart")
|
|
|
|
.field("text", &self.text)
|
|
|
|
.field("linked_location", &self.linked_location)
|
|
|
|
.finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 04:29:19 -05:00
|
|
|
// Feature: Inlay Hints
|
|
|
|
//
|
|
|
|
// rust-analyzer shows additional information inline with the source code.
|
|
|
|
// Editors usually render this using read-only virtual text snippets interspersed with code.
|
|
|
|
//
|
2022-03-18 12:11:16 -05:00
|
|
|
// rust-analyzer by default shows hints for
|
2020-05-31 04:29:19 -05:00
|
|
|
//
|
|
|
|
// * types of local variables
|
|
|
|
// * names of function arguments
|
|
|
|
// * types of chained expressions
|
|
|
|
//
|
2022-03-18 12:11:16 -05:00
|
|
|
// Optionally, one can enable additional hints for
|
|
|
|
//
|
2022-05-28 07:13:25 -05:00
|
|
|
// * return types of closure expressions
|
2022-03-19 13:01:19 -05:00
|
|
|
// * elided lifetimes
|
2022-03-20 08:41:27 -05:00
|
|
|
// * compiler inserted reborrows
|
2020-05-31 04:29:19 -05:00
|
|
|
//
|
2021-03-30 18:08:10 -05:00
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113020660-b5f98b80-917a-11eb-8d70-3be3fd558cdd.png[]
|
2019-11-18 11:02:28 -06:00
|
|
|
pub(crate) fn inlay_hints(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
2022-11-07 10:21:37 -06:00
|
|
|
range_limit: Option<TextRange>,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2019-11-18 11:02:28 -06:00
|
|
|
) -> Vec<InlayHint> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("inlay_hints");
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(file_id);
|
2021-09-14 13:30:28 -05:00
|
|
|
let file = file.syntax();
|
2020-02-29 16:24:50 -06:00
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
let mut acc = Vec::new();
|
2022-02-11 16:48:01 -06:00
|
|
|
|
2022-05-30 06:51:48 -05:00
|
|
|
if let Some(scope) = sema.scope(&file) {
|
|
|
|
let famous_defs = FamousDefs(&sema, scope.krate());
|
|
|
|
|
|
|
|
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
|
|
|
|
match range_limit {
|
2022-11-07 10:21:37 -06:00
|
|
|
Some(range) => match file.covering_element(range) {
|
2022-05-30 06:51:48 -05:00
|
|
|
NodeOrToken::Token(_) => return acc,
|
|
|
|
NodeOrToken::Node(n) => n
|
|
|
|
.descendants()
|
|
|
|
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
|
|
|
|
.for_each(hints),
|
|
|
|
},
|
|
|
|
None => file.descendants().for_each(hints),
|
|
|
|
};
|
|
|
|
}
|
2022-02-11 16:48:01 -06:00
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
acc
|
2022-02-11 16:48:01 -06:00
|
|
|
}
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
fn hints(
|
2022-02-11 16:48:01 -06:00
|
|
|
hints: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
|
2022-02-11 16:48:01 -06:00
|
|
|
config: &InlayHintsConfig,
|
2022-05-19 06:38:37 -05:00
|
|
|
file_id: FileId,
|
2022-02-11 16:48:01 -06:00
|
|
|
node: SyntaxNode,
|
|
|
|
) {
|
2022-05-19 06:38:37 -05:00
|
|
|
closing_brace_hints(hints, sema, config, file_id, node.clone());
|
2022-05-17 05:18:07 -05:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::Expr(expr) => {
|
2022-05-19 06:38:37 -05:00
|
|
|
chaining_hints(hints, sema, &famous_defs, config, file_id, &expr);
|
2022-11-04 11:11:15 -05:00
|
|
|
adjustment_hints(hints, sema, config, &expr);
|
2022-05-17 05:18:07 -05:00
|
|
|
match expr {
|
|
|
|
ast::Expr::CallExpr(it) => param_name_hints(hints, sema, config, ast::Expr::from(it)),
|
|
|
|
ast::Expr::MethodCallExpr(it) => {
|
|
|
|
param_name_hints(hints, sema, config, ast::Expr::from(it))
|
|
|
|
}
|
2022-05-19 06:38:37 -05:00
|
|
|
ast::Expr::ClosureExpr(it) => closure_ret_hints(hints, sema, &famous_defs, config, file_id, it),
|
2022-05-17 05:18:07 -05:00
|
|
|
// We could show reborrows for all expressions, but usually that is just noise to the user
|
|
|
|
// and the main point here is to show why "moving" a mutable reference doesn't necessarily move it
|
2022-11-04 11:11:15 -05:00
|
|
|
// ast::Expr::PathExpr(_) => reborrow_hints(hints, sema, config, &expr),
|
2022-05-17 05:18:07 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::Pat(it) => {
|
|
|
|
binding_mode_hints(hints, sema, config, &it);
|
|
|
|
if let ast::Pat::IdentPat(it) = it {
|
2022-05-19 06:38:37 -05:00
|
|
|
bind_pat_hints(hints, sema, config, file_id, &it);
|
2022-05-17 05:18:07 -05:00
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
},
|
2022-05-30 06:51:48 -05:00
|
|
|
ast::Item(it) => match it {
|
|
|
|
// FIXME: record impl lifetimes so they aren't being reused in assoc item lifetime inlay hints
|
|
|
|
ast::Item::Impl(_) => None,
|
|
|
|
ast::Item::Fn(it) => fn_lifetime_fn_hints(hints, config, it),
|
|
|
|
// static type elisions
|
|
|
|
ast::Item::Static(it) => implicit_static_hints(hints, config, Either::Left(it)),
|
|
|
|
ast::Item::Const(it) => implicit_static_hints(hints, config, Either::Right(it)),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
// FIXME: fn-ptr type, dyn fn type, and trait object type elisions
|
|
|
|
ast::Type(_) => None,
|
|
|
|
_ => None,
|
2022-05-14 07:26:08 -05:00
|
|
|
}
|
2022-05-17 05:18:07 -05:00
|
|
|
};
|
2019-07-19 16:20:09 -05:00
|
|
|
}
|
|
|
|
|
2022-05-13 12:42:59 -05:00
|
|
|
fn closing_brace_hints(
|
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2022-05-13 12:42:59 -05:00
|
|
|
config: &InlayHintsConfig,
|
2022-05-19 06:38:37 -05:00
|
|
|
file_id: FileId,
|
2022-05-13 12:42:59 -05:00
|
|
|
node: SyntaxNode,
|
|
|
|
) -> Option<()> {
|
|
|
|
let min_lines = config.closing_brace_hints_min_lines?;
|
|
|
|
|
2022-08-31 11:54:19 -05:00
|
|
|
let name = |it: ast::Name| it.syntax().text_range();
|
2022-05-17 06:39:45 -05:00
|
|
|
|
2022-05-13 12:42:59 -05:00
|
|
|
let mut closing_token;
|
2022-08-31 11:54:19 -05:00
|
|
|
let (label, name_range) = if let Some(item_list) = ast::AssocItemList::cast(node.clone()) {
|
2022-05-13 12:42:59 -05:00
|
|
|
closing_token = item_list.r_curly_token()?;
|
|
|
|
|
|
|
|
let parent = item_list.syntax().parent()?;
|
|
|
|
match_ast! {
|
|
|
|
match parent {
|
|
|
|
ast::Impl(imp) => {
|
|
|
|
let imp = sema.to_def(&imp)?;
|
|
|
|
let ty = imp.self_ty(sema.db);
|
|
|
|
let trait_ = imp.trait_(sema.db);
|
2022-08-31 11:54:19 -05:00
|
|
|
let hint_text = match trait_ {
|
2022-05-13 12:42:59 -05:00
|
|
|
Some(tr) => format!("impl {} for {}", tr.name(sema.db), ty.display_truncated(sema.db, config.max_length)),
|
|
|
|
None => format!("impl {}", ty.display_truncated(sema.db, config.max_length)),
|
2022-08-31 11:54:19 -05:00
|
|
|
};
|
|
|
|
(hint_text, None)
|
2022-05-13 12:42:59 -05:00
|
|
|
},
|
|
|
|
ast::Trait(tr) => {
|
2022-05-17 06:39:45 -05:00
|
|
|
(format!("trait {}", tr.name()?), tr.name().map(name))
|
2022-05-13 12:42:59 -05:00
|
|
|
},
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(list) = ast::ItemList::cast(node.clone()) {
|
|
|
|
closing_token = list.r_curly_token()?;
|
|
|
|
|
|
|
|
let module = ast::Module::cast(list.syntax().parent()?)?;
|
2022-05-17 06:39:45 -05:00
|
|
|
(format!("mod {}", module.name()?), module.name().map(name))
|
2022-05-13 12:42:59 -05:00
|
|
|
} else if let Some(block) = ast::BlockExpr::cast(node.clone()) {
|
|
|
|
closing_token = block.stmt_list()?.r_curly_token()?;
|
|
|
|
|
|
|
|
let parent = block.syntax().parent()?;
|
|
|
|
match_ast! {
|
|
|
|
match parent {
|
|
|
|
ast::Fn(it) => {
|
|
|
|
// FIXME: this could include parameters, but `HirDisplay` prints too much info
|
|
|
|
// and doesn't respect the max length either, so the hints end up way too long
|
2022-05-17 06:39:45 -05:00
|
|
|
(format!("fn {}", it.name()?), it.name().map(name))
|
2022-05-13 12:42:59 -05:00
|
|
|
},
|
2022-05-17 06:39:45 -05:00
|
|
|
ast::Static(it) => (format!("static {}", it.name()?), it.name().map(name)),
|
2022-05-13 12:42:59 -05:00
|
|
|
ast::Const(it) => {
|
|
|
|
if it.underscore_token().is_some() {
|
2022-05-17 06:39:45 -05:00
|
|
|
("const _".into(), None)
|
2022-05-13 12:42:59 -05:00
|
|
|
} else {
|
2022-05-17 06:39:45 -05:00
|
|
|
(format!("const {}", it.name()?), it.name().map(name))
|
2022-05-13 12:42:59 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
2022-05-16 08:23:25 -05:00
|
|
|
} else if let Some(mac) = ast::MacroCall::cast(node.clone()) {
|
|
|
|
let last_token = mac.syntax().last_token()?;
|
|
|
|
if last_token.kind() != T![;] && last_token.kind() != SyntaxKind::R_CURLY {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
closing_token = last_token;
|
|
|
|
|
2022-05-17 06:39:45 -05:00
|
|
|
(
|
|
|
|
format!("{}!", mac.path()?),
|
2022-08-31 11:54:19 -05:00
|
|
|
mac.path().and_then(|it| it.segment()).map(|it| it.syntax().text_range()),
|
2022-05-17 06:39:45 -05:00
|
|
|
)
|
2022-05-13 12:42:59 -05:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(mut next) = closing_token.next_token() {
|
|
|
|
if next.kind() == T![;] {
|
|
|
|
if let Some(tok) = next.next_token() {
|
|
|
|
closing_token = next;
|
|
|
|
next = tok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !(next.kind() == SyntaxKind::WHITESPACE && next.text().contains('\n')) {
|
|
|
|
// Only display the hint if the `}` is the last token on the line
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut lines = 1;
|
|
|
|
node.text().for_each_chunk(|s| lines += s.matches('\n').count());
|
|
|
|
if lines < min_lines {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:54:19 -05:00
|
|
|
let linked_location = name_range.map(|range| FileRange { file_id, range });
|
2022-05-13 12:42:59 -05:00
|
|
|
acc.push(InlayHint {
|
|
|
|
range: closing_token.text_range(),
|
2022-05-17 07:46:43 -05:00
|
|
|
kind: InlayKind::ClosingBraceHint,
|
2022-08-31 11:54:19 -05:00
|
|
|
label: InlayHintLabel { parts: vec![InlayHintLabelPart { text: label, linked_location }] },
|
|
|
|
tooltip: None, // provided by label part location
|
2022-05-13 12:42:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-05-30 06:51:48 -05:00
|
|
|
fn implicit_static_hints(
|
|
|
|
acc: &mut Vec<InlayHint>,
|
|
|
|
config: &InlayHintsConfig,
|
|
|
|
statik_or_const: Either<ast::Static, ast::Const>,
|
|
|
|
) -> Option<()> {
|
|
|
|
if config.lifetime_elision_hints != LifetimeElisionHints::Always {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Either::Right(it) = &statik_or_const {
|
|
|
|
if ast::AssocItemList::can_cast(
|
|
|
|
it.syntax().parent().map_or(SyntaxKind::EOF, |it| it.kind()),
|
|
|
|
) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ast::Type::RefType(ty)) = statik_or_const.either(|it| it.ty(), |it| it.ty()) {
|
|
|
|
if ty.lifetime().is_none() {
|
|
|
|
let t = ty.amp_token()?;
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: t.text_range(),
|
|
|
|
kind: InlayKind::LifetimeHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: "'static".to_owned().into(),
|
2022-05-30 06:51:48 -05:00
|
|
|
tooltip: Some(InlayTooltip::String("Elided static lifetime".into())),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_lifetime_fn_hints(
|
2022-03-18 12:11:16 -05:00
|
|
|
acc: &mut Vec<InlayHint>,
|
|
|
|
config: &InlayHintsConfig,
|
|
|
|
func: ast::Fn,
|
|
|
|
) -> Option<()> {
|
2022-03-19 13:01:19 -05:00
|
|
|
if config.lifetime_elision_hints == LifetimeElisionHints::Never {
|
2022-03-18 12:11:16 -05:00
|
|
|
return None;
|
|
|
|
}
|
2022-05-17 05:56:14 -05:00
|
|
|
|
2022-08-31 11:34:10 -05:00
|
|
|
let mk_lt_hint = |t: SyntaxToken, label: String| InlayHint {
|
2022-05-17 05:56:14 -05:00
|
|
|
range: t.text_range(),
|
|
|
|
kind: InlayKind::LifetimeHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: label.into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::String("Elided lifetime".into())),
|
2022-05-17 05:56:14 -05:00
|
|
|
};
|
|
|
|
|
2022-03-18 12:11:16 -05:00
|
|
|
let param_list = func.param_list()?;
|
|
|
|
let generic_param_list = func.generic_param_list();
|
|
|
|
let ret_type = func.ret_type();
|
2022-03-18 12:57:11 -05:00
|
|
|
let self_param = param_list.self_param().filter(|it| it.amp_token().is_some());
|
2022-03-18 12:11:16 -05:00
|
|
|
|
2022-05-17 05:18:07 -05:00
|
|
|
let is_elided = |lt: &Option<ast::Lifetime>| match lt {
|
|
|
|
Some(lt) => matches!(lt.text().as_str(), "'_"),
|
|
|
|
None => true,
|
2022-03-18 12:11:16 -05:00
|
|
|
};
|
|
|
|
|
2022-05-17 05:18:07 -05:00
|
|
|
let potential_lt_refs = {
|
|
|
|
let mut acc: Vec<_> = vec![];
|
|
|
|
if let Some(self_param) = &self_param {
|
|
|
|
let lifetime = self_param.lifetime();
|
|
|
|
let is_elided = is_elided(&lifetime);
|
|
|
|
acc.push((None, self_param.amp_token(), lifetime, is_elided));
|
|
|
|
}
|
|
|
|
param_list.params().filter_map(|it| Some((it.pat(), it.ty()?))).for_each(|(pat, ty)| {
|
2022-03-19 13:01:19 -05:00
|
|
|
// FIXME: check path types
|
|
|
|
walk_ty(&ty, &mut |ty| match ty {
|
2022-05-17 05:18:07 -05:00
|
|
|
ast::Type::RefType(r) => {
|
|
|
|
let lifetime = r.lifetime();
|
|
|
|
let is_elided = is_elided(&lifetime);
|
|
|
|
acc.push((
|
|
|
|
pat.as_ref().and_then(|it| match it {
|
|
|
|
ast::Pat::IdentPat(p) => p.name(),
|
|
|
|
_ => None,
|
|
|
|
}),
|
|
|
|
r.amp_token(),
|
|
|
|
lifetime,
|
|
|
|
is_elided,
|
|
|
|
))
|
|
|
|
}
|
2022-03-19 13:01:19 -05:00
|
|
|
_ => (),
|
|
|
|
})
|
|
|
|
});
|
2022-05-17 05:18:07 -05:00
|
|
|
acc
|
2022-03-18 12:11:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// allocate names
|
2022-05-17 05:18:07 -05:00
|
|
|
let mut gen_idx_name = {
|
|
|
|
let mut gen = (0u8..).map(|idx| match idx {
|
|
|
|
idx if idx < 10 => SmolStr::from_iter(['\'', (idx + 48) as char]),
|
|
|
|
idx => format!("'{idx}").into(),
|
|
|
|
});
|
|
|
|
move || gen.next().unwrap_or_default()
|
|
|
|
};
|
|
|
|
let mut allocated_lifetimes = vec![];
|
|
|
|
|
|
|
|
let mut used_names: FxHashMap<SmolStr, usize> =
|
|
|
|
match config.param_names_for_lifetime_elision_hints {
|
|
|
|
true => generic_param_list
|
|
|
|
.iter()
|
|
|
|
.flat_map(|gpl| gpl.lifetime_params())
|
|
|
|
.filter_map(|param| param.lifetime())
|
|
|
|
.filter_map(|lt| Some((SmolStr::from(lt.text().as_str().get(1..)?), 0)))
|
|
|
|
.collect(),
|
|
|
|
false => Default::default(),
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let mut potential_lt_refs = potential_lt_refs.iter().filter(|&&(.., is_elided)| is_elided);
|
|
|
|
if let Some(_) = &self_param {
|
|
|
|
if let Some(_) = potential_lt_refs.next() {
|
|
|
|
allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
|
|
|
|
// self can't be used as a lifetime, so no need to check for collisions
|
|
|
|
"'self".into()
|
|
|
|
} else {
|
|
|
|
gen_idx_name()
|
|
|
|
});
|
|
|
|
}
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
2022-05-17 05:18:07 -05:00
|
|
|
potential_lt_refs.for_each(|(name, ..)| {
|
2022-03-19 13:01:19 -05:00
|
|
|
let name = match name {
|
2022-05-17 05:18:07 -05:00
|
|
|
Some(it) if config.param_names_for_lifetime_elision_hints => {
|
2022-03-19 13:01:19 -05:00
|
|
|
if let Some(c) = used_names.get_mut(it.text().as_str()) {
|
|
|
|
*c += 1;
|
|
|
|
SmolStr::from(format!("'{text}{c}", text = it.text().as_str()))
|
|
|
|
} else {
|
|
|
|
used_names.insert(it.text().as_str().into(), 0);
|
|
|
|
SmolStr::from_iter(["\'", it.text().as_str()])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => gen_idx_name(),
|
|
|
|
};
|
|
|
|
allocated_lifetimes.push(name);
|
2022-05-17 05:18:07 -05:00
|
|
|
});
|
|
|
|
}
|
2022-03-18 12:11:16 -05:00
|
|
|
|
|
|
|
// fetch output lifetime if elision rule applies
|
2022-05-17 05:18:07 -05:00
|
|
|
let output = match potential_lt_refs.as_slice() {
|
|
|
|
[(_, _, lifetime, _), ..] if self_param.is_some() || potential_lt_refs.len() == 1 => {
|
|
|
|
match lifetime {
|
|
|
|
Some(lt) => match lt.text().as_str() {
|
|
|
|
"'_" => allocated_lifetimes.get(0).cloned(),
|
|
|
|
"'static" => None,
|
|
|
|
name => Some(name.into()),
|
|
|
|
},
|
|
|
|
None => allocated_lifetimes.get(0).cloned(),
|
|
|
|
}
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
2022-05-17 05:18:07 -05:00
|
|
|
[..] => None,
|
2022-03-18 12:11:16 -05:00
|
|
|
};
|
|
|
|
|
2022-03-19 13:01:19 -05:00
|
|
|
if allocated_lifetimes.is_empty() && output.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2022-03-18 12:11:16 -05:00
|
|
|
|
2022-03-19 13:01:19 -05:00
|
|
|
// apply hints
|
2022-03-18 12:11:16 -05:00
|
|
|
// apply output if required
|
2022-03-19 14:12:14 -05:00
|
|
|
let mut is_trivial = true;
|
|
|
|
if let (Some(output_lt), Some(r)) = (&output, ret_type) {
|
|
|
|
if let Some(ty) = r.ty() {
|
|
|
|
walk_ty(&ty, &mut |ty| match ty {
|
|
|
|
ast::Type::RefType(ty) if ty.lifetime().is_none() => {
|
|
|
|
if let Some(amp) = ty.amp_token() {
|
|
|
|
is_trivial = false;
|
2022-05-17 05:56:14 -05:00
|
|
|
acc.push(mk_lt_hint(amp, output_lt.to_string()));
|
2022-03-19 12:11:56 -05:00
|
|
|
}
|
2022-03-19 14:12:14 -05:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
})
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
2022-03-19 14:12:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial {
|
|
|
|
return None;
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
|
|
|
|
2022-05-17 05:18:07 -05:00
|
|
|
let mut a = allocated_lifetimes.iter();
|
|
|
|
for (_, amp_token, _, is_elided) in potential_lt_refs {
|
|
|
|
if is_elided {
|
|
|
|
let t = amp_token?;
|
2022-05-17 05:56:14 -05:00
|
|
|
let lt = a.next()?;
|
|
|
|
acc.push(mk_lt_hint(t, lt.to_string()));
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate generic param list things
|
|
|
|
match (generic_param_list, allocated_lifetimes.as_slice()) {
|
|
|
|
(_, []) => (),
|
|
|
|
(Some(gpl), allocated_lifetimes) => {
|
|
|
|
let angle_tok = gpl.l_angle_token()?;
|
|
|
|
let is_empty = gpl.generic_params().next().is_none();
|
2022-05-19 06:38:37 -05:00
|
|
|
acc.push(InlayHint {
|
|
|
|
range: angle_tok.text_range(),
|
|
|
|
kind: InlayKind::LifetimeHint,
|
|
|
|
label: format!(
|
2022-03-18 12:11:16 -05:00
|
|
|
"{}{}",
|
|
|
|
allocated_lifetimes.iter().format(", "),
|
|
|
|
if is_empty { "" } else { ", " }
|
2022-08-31 11:34:10 -05:00
|
|
|
)
|
|
|
|
.into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
|
|
|
|
});
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
|
|
|
(None, allocated_lifetimes) => acc.push(InlayHint {
|
|
|
|
range: func.name()?.syntax().text_range(),
|
|
|
|
kind: InlayKind::GenericParamListHint,
|
|
|
|
label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
|
2022-03-18 12:11:16 -05:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
fn closure_ret_hints(
|
2020-03-24 13:33:00 -05:00
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
famous_defs: &FamousDefs<'_, '_>,
|
2022-03-16 15:16:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2022-05-19 06:38:37 -05:00
|
|
|
file_id: FileId,
|
2022-03-16 15:16:55 -05:00
|
|
|
closure: ast::ClosureExpr,
|
|
|
|
) -> Option<()> {
|
2022-05-28 07:13:25 -05:00
|
|
|
if config.closure_return_type_hints == ClosureReturnTypeHints::Never {
|
2022-03-16 15:16:55 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:35:37 -05:00
|
|
|
if closure.ret_type().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-05-28 07:13:25 -05:00
|
|
|
if !closure_has_block_body(&closure)
|
|
|
|
&& config.closure_return_type_hints == ClosureReturnTypeHints::WithBlock
|
|
|
|
{
|
2022-05-15 06:17:52 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let param_list = closure.param_list()?;
|
2022-03-19 14:24:09 -05:00
|
|
|
|
|
|
|
let closure = sema.descend_node_into_attributes(closure.clone()).pop()?;
|
2022-03-16 15:16:55 -05:00
|
|
|
let ty = sema.type_of_expr(&ast::Expr::ClosureExpr(closure))?.adjusted();
|
|
|
|
let callable = ty.as_callable(sema.db)?;
|
|
|
|
let ty = callable.return_type();
|
|
|
|
if ty.is_unit() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: param_list.syntax().text_range(),
|
|
|
|
kind: InlayKind::ClosureReturnTypeHint,
|
|
|
|
label: hint_iterator(sema, &famous_defs, config, &ty)
|
2022-08-31 11:34:10 -05:00
|
|
|
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string())
|
|
|
|
.into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::HoverRanged(file_id, param_list.syntax().text_range())),
|
2022-03-16 15:16:55 -05:00
|
|
|
});
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-11-04 11:11:15 -05:00
|
|
|
fn adjustment_hints(
|
2022-03-20 08:38:16 -05:00
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2022-03-20 08:38:16 -05:00
|
|
|
config: &InlayHintsConfig,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
) -> Option<()> {
|
2022-11-04 11:11:15 -05:00
|
|
|
if config.adjustment_hints == AdjustmentHints::Never {
|
2022-11-04 16:40:06 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:40:54 -06:00
|
|
|
// These inherit from the inner expression which would result in duplicate hints
|
|
|
|
if let ast::Expr::ParenExpr(_)
|
|
|
|
| ast::Expr::IfExpr(_)
|
|
|
|
| ast::Expr::BlockExpr(_)
|
|
|
|
| ast::Expr::MatchExpr(_) = expr
|
|
|
|
{
|
2022-11-04 16:40:06 -05:00
|
|
|
return None;
|
2022-03-20 08:38:16 -05:00
|
|
|
}
|
|
|
|
|
2022-11-04 11:11:15 -05:00
|
|
|
let parent = expr.syntax().parent().and_then(ast::Expr::cast);
|
2022-05-14 08:00:14 -05:00
|
|
|
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
|
|
|
|
let desc_expr = descended.as_ref().unwrap_or(expr);
|
2022-11-04 11:11:15 -05:00
|
|
|
let adjustments = sema.expr_adjustments(desc_expr).filter(|it| !it.is_empty())?;
|
|
|
|
let needs_parens = match parent {
|
|
|
|
Some(parent) => {
|
|
|
|
match parent {
|
|
|
|
ast::Expr::AwaitExpr(_)
|
|
|
|
| ast::Expr::CallExpr(_)
|
|
|
|
| ast::Expr::CastExpr(_)
|
|
|
|
| ast::Expr::FieldExpr(_)
|
|
|
|
| ast::Expr::MethodCallExpr(_)
|
|
|
|
| ast::Expr::TryExpr(_) => true,
|
|
|
|
// FIXME: shorthands need special casing, though not sure if adjustments are even valid there
|
|
|
|
ast::Expr::RecordExpr(_) => false,
|
|
|
|
ast::Expr::IndexExpr(index) => index.base().as_ref() == Some(expr),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => false,
|
2022-05-12 06:39:32 -05:00
|
|
|
};
|
2022-11-04 11:11:15 -05:00
|
|
|
if needs_parens {
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: expr.syntax().text_range(),
|
2022-12-16 13:52:31 -06:00
|
|
|
kind: InlayKind::OpeningParenthesis,
|
2022-11-04 11:11:15 -05:00
|
|
|
label: "(".into(),
|
|
|
|
tooltip: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for adjustment in adjustments.into_iter().rev() {
|
|
|
|
// FIXME: Add some nicer tooltips to each of these
|
|
|
|
let text = match adjustment {
|
2022-11-04 16:59:07 -05:00
|
|
|
Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
|
|
|
|
"<never-to-any>"
|
|
|
|
}
|
2022-11-04 11:11:15 -05:00
|
|
|
Adjust::Deref(None) => "*",
|
|
|
|
Adjust::Deref(Some(OverloadedDeref(Mutability::Mut))) => "*",
|
|
|
|
Adjust::Deref(Some(OverloadedDeref(Mutability::Shared))) => "*",
|
|
|
|
Adjust::Borrow(AutoBorrow::Ref(Mutability::Shared)) => "&",
|
|
|
|
Adjust::Borrow(AutoBorrow::Ref(Mutability::Mut)) => "&mut ",
|
|
|
|
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Shared)) => "&raw const ",
|
|
|
|
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Mut)) => "&raw mut ",
|
|
|
|
// some of these could be represented via `as` casts, but that's not too nice and
|
|
|
|
// handling everything as a prefix expr makes the `(` and `)` insertion easier
|
2022-11-04 16:59:07 -05:00
|
|
|
Adjust::Pointer(cast) if config.adjustment_hints == AdjustmentHints::Always => {
|
|
|
|
match cast {
|
|
|
|
PointerCast::ReifyFnPointer => "<fn-item-to-fn-pointer>",
|
|
|
|
PointerCast::UnsafeFnPointer => "<safe-fn-pointer-to-unsafe-fn-pointer>",
|
|
|
|
PointerCast::ClosureFnPointer(Safety::Unsafe) => {
|
|
|
|
"<closure-to-unsafe-fn-pointer>"
|
|
|
|
}
|
|
|
|
PointerCast::ClosureFnPointer(Safety::Safe) => "<closure-to-fn-pointer>",
|
|
|
|
PointerCast::MutToConstPointer => "<mut-ptr-to-const-ptr>",
|
|
|
|
PointerCast::ArrayToPointer => "<array-ptr-to-element-ptr>",
|
|
|
|
PointerCast::Unsize => "<unsize>",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => continue,
|
2022-11-04 11:11:15 -05:00
|
|
|
};
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: expr.syntax().text_range(),
|
|
|
|
kind: InlayKind::AdjustmentHint,
|
|
|
|
label: text.into(),
|
|
|
|
tooltip: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if needs_parens {
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: expr.syntax().text_range(),
|
2022-12-16 13:52:31 -06:00
|
|
|
kind: InlayKind::ClosingParenthesis,
|
2022-11-04 11:11:15 -05:00
|
|
|
label: ")".into(),
|
|
|
|
tooltip: None,
|
|
|
|
});
|
|
|
|
}
|
2022-03-20 08:38:16 -05:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
fn chaining_hints(
|
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
famous_defs: &FamousDefs<'_, '_>,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2022-05-19 06:38:37 -05:00
|
|
|
file_id: FileId,
|
2021-09-13 18:59:45 -05:00
|
|
|
expr: &ast::Expr,
|
2020-03-24 13:33:00 -05:00
|
|
|
) -> Option<()> {
|
2020-03-31 09:02:55 -05:00
|
|
|
if !config.chaining_hints {
|
2020-03-24 13:33:00 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:21:30 -05:00
|
|
|
if matches!(expr, ast::Expr::RecordExpr(_)) {
|
2020-03-24 13:33:00 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-18 06:19:29 -05:00
|
|
|
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
|
|
|
|
let desc_expr = descended.as_ref().unwrap_or(expr);
|
2020-10-20 10:38:21 -05:00
|
|
|
|
2020-03-24 14:31:02 -05:00
|
|
|
let mut tokens = expr
|
|
|
|
.syntax()
|
2020-03-24 13:33:00 -05:00
|
|
|
.siblings_with_tokens(Direction::Next)
|
|
|
|
.filter_map(NodeOrToken::into_token)
|
|
|
|
.filter(|t| match t.kind() {
|
|
|
|
SyntaxKind::WHITESPACE if !t.text().contains('\n') => false,
|
|
|
|
SyntaxKind::COMMENT => false,
|
|
|
|
_ => true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chaining can be defined as an expression whose next sibling tokens are newline and dot
|
|
|
|
// Ignoring extra whitespace and comments
|
|
|
|
let next = tokens.next()?.kind();
|
2021-02-16 17:09:31 -06:00
|
|
|
if next == SyntaxKind::WHITESPACE {
|
|
|
|
let mut next_next = tokens.next()?.kind();
|
|
|
|
while next_next == SyntaxKind::WHITESPACE {
|
|
|
|
next_next = tokens.next()?.kind();
|
2020-03-31 13:25:03 -05:00
|
|
|
}
|
2021-02-16 17:09:31 -06:00
|
|
|
if next_next == T![.] {
|
2021-09-18 06:19:29 -05:00
|
|
|
let ty = sema.type_of_expr(desc_expr)?.original;
|
2021-02-16 17:09:31 -06:00
|
|
|
if ty.is_unknown() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if matches!(expr, ast::Expr::PathExpr(_)) {
|
|
|
|
if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
|
|
|
|
if st.fields(sema.db).is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-03-31 13:25:03 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-16 17:09:31 -06:00
|
|
|
acc.push(InlayHint {
|
2021-09-18 06:19:29 -05:00
|
|
|
range: expr.syntax().text_range(),
|
2021-02-16 17:09:31 -06:00
|
|
|
kind: InlayKind::ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: hint_iterator(sema, &famous_defs, config, &ty)
|
|
|
|
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string())
|
|
|
|
.into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::HoverRanged(file_id, expr.syntax().text_range())),
|
2021-02-16 17:09:31 -06:00
|
|
|
});
|
2020-03-31 13:25:03 -05:00
|
|
|
}
|
2020-03-24 13:33:00 -05:00
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
fn param_name_hints(
|
2020-01-16 07:31:34 -06:00
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2020-02-29 16:24:50 -06:00
|
|
|
expr: ast::Expr,
|
2020-01-16 07:31:34 -06:00
|
|
|
) -> Option<()> {
|
2020-03-31 09:02:55 -05:00
|
|
|
if !config.parameter_hints {
|
2020-03-10 13:21:56 -05:00
|
|
|
return None;
|
2020-03-10 02:55:46 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:22:24 -05:00
|
|
|
let (callable, arg_list) = get_callable(sema, &expr)?;
|
2020-07-16 14:51:44 -05:00
|
|
|
let hints = callable
|
|
|
|
.params(sema.db)
|
|
|
|
.into_iter()
|
2021-06-03 09:22:24 -05:00
|
|
|
.zip(arg_list.args())
|
2020-08-12 16:43:01 -05:00
|
|
|
.filter_map(|((param, _ty), arg)| {
|
2021-09-18 06:19:29 -05:00
|
|
|
// Only annotate hints for expressions that exist in the original file
|
|
|
|
let range = sema.original_range_opt(arg.syntax())?;
|
2022-05-19 07:32:09 -05:00
|
|
|
let (param_name, name_syntax) = match param.as_ref()? {
|
|
|
|
Either::Left(pat) => ("self".to_string(), pat.name()),
|
2020-08-12 16:43:01 -05:00
|
|
|
Either::Right(pat) => match pat {
|
2022-05-19 07:32:09 -05:00
|
|
|
ast::Pat::IdentPat(it) => (it.name()?.to_string(), it.name()),
|
2020-08-12 16:43:01 -05:00
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
};
|
2022-05-19 07:32:09 -05:00
|
|
|
Some((name_syntax, param_name, arg, range))
|
2020-07-16 14:51:44 -05:00
|
|
|
})
|
2022-05-19 06:38:37 -05:00
|
|
|
.filter(|(_, param_name, arg, _)| {
|
2021-09-18 06:19:29 -05:00
|
|
|
!should_hide_param_name_hint(sema, &callable, param_name, arg)
|
|
|
|
})
|
2022-05-19 07:32:09 -05:00
|
|
|
.map(|(param, param_name, _, FileRange { range, .. })| {
|
|
|
|
let mut tooltip = None;
|
|
|
|
if let Some(name) = param {
|
|
|
|
if let hir::CallableKind::Function(f) = callable.kind() {
|
|
|
|
// assert the file is cached so we can map out of macros
|
|
|
|
if let Some(_) = sema.source(f) {
|
|
|
|
tooltip = sema.original_range_opt(name.syntax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InlayHint {
|
|
|
|
range,
|
|
|
|
kind: InlayKind::ParameterHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: param_name.into(),
|
2022-05-19 07:32:09 -05:00
|
|
|
tooltip: tooltip.map(|it| InlayTooltip::HoverOffset(it.file_id, it.range.start())),
|
|
|
|
}
|
2020-02-29 16:24:50 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
acc.extend(hints);
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-05-14 07:26:08 -05:00
|
|
|
fn binding_mode_hints(
|
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2022-05-14 07:26:08 -05:00
|
|
|
config: &InlayHintsConfig,
|
|
|
|
pat: &ast::Pat,
|
|
|
|
) -> Option<()> {
|
|
|
|
if !config.binding_mode_hints {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-12-16 14:16:55 -06:00
|
|
|
let outer_paren_pat = pat
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.skip(1)
|
|
|
|
.map_while(ast::Pat::cast)
|
|
|
|
.map_while(|pat| match pat {
|
|
|
|
ast::Pat::ParenPat(pat) => Some(pat),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.last();
|
|
|
|
let range =
|
|
|
|
outer_paren_pat.as_ref().map_or_else(|| pat.syntax(), |it| it.syntax()).text_range();
|
2022-05-14 07:26:08 -05:00
|
|
|
sema.pattern_adjustments(&pat).iter().for_each(|ty| {
|
|
|
|
let reference = ty.is_reference();
|
|
|
|
let mut_reference = ty.is_mutable_reference();
|
|
|
|
let r = match (reference, mut_reference) {
|
|
|
|
(true, true) => "&mut",
|
|
|
|
(true, false) => "&",
|
|
|
|
_ => return,
|
|
|
|
};
|
2022-05-17 07:46:43 -05:00
|
|
|
acc.push(InlayHint {
|
|
|
|
range,
|
|
|
|
kind: InlayKind::BindingModeHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: r.to_string().into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: Some(InlayTooltip::String("Inferred binding mode".into())),
|
2022-05-17 07:46:43 -05:00
|
|
|
});
|
2022-05-14 07:26:08 -05:00
|
|
|
});
|
2022-11-07 05:49:52 -06:00
|
|
|
match pat {
|
|
|
|
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
|
|
|
|
let bm = sema.binding_mode_of_pat(pat)?;
|
|
|
|
let bm = match bm {
|
|
|
|
hir::BindingMode::Move => return None,
|
|
|
|
hir::BindingMode::Ref(Mutability::Mut) => "ref mut",
|
|
|
|
hir::BindingMode::Ref(Mutability::Shared) => "ref",
|
|
|
|
};
|
|
|
|
acc.push(InlayHint {
|
2022-12-16 14:16:55 -06:00
|
|
|
range: pat.syntax().text_range(),
|
2022-11-07 05:49:52 -06:00
|
|
|
kind: InlayKind::BindingModeHint,
|
|
|
|
label: bm.to_string().into(),
|
|
|
|
tooltip: Some(InlayTooltip::String("Inferred binding mode".into())),
|
|
|
|
});
|
|
|
|
}
|
2022-12-16 14:16:55 -06:00
|
|
|
ast::Pat::OrPat(pat) if outer_paren_pat.is_none() => {
|
2022-12-16 13:52:31 -06:00
|
|
|
acc.push(InlayHint {
|
|
|
|
range: pat.syntax().text_range(),
|
|
|
|
kind: InlayKind::OpeningParenthesis,
|
|
|
|
label: "(".into(),
|
|
|
|
tooltip: None,
|
|
|
|
});
|
|
|
|
acc.push(InlayHint {
|
|
|
|
range: pat.syntax().text_range(),
|
|
|
|
kind: InlayKind::ClosingParenthesis,
|
|
|
|
label: ")".into(),
|
|
|
|
tooltip: None,
|
|
|
|
});
|
|
|
|
}
|
2022-11-07 05:49:52 -06:00
|
|
|
_ => (),
|
|
|
|
}
|
2022-05-14 07:26:08 -05:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
fn bind_pat_hints(
|
2020-02-29 16:24:50 -06:00
|
|
|
acc: &mut Vec<InlayHint>,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-03-31 09:02:55 -05:00
|
|
|
config: &InlayHintsConfig,
|
2022-05-19 06:38:37 -05:00
|
|
|
file_id: FileId,
|
2021-09-18 06:19:29 -05:00
|
|
|
pat: &ast::IdentPat,
|
2020-02-29 16:24:50 -06:00
|
|
|
) -> Option<()> {
|
2020-03-31 09:02:55 -05:00
|
|
|
if !config.type_hints {
|
2020-03-10 13:21:56 -05:00
|
|
|
return None;
|
2020-03-10 02:55:46 -05:00
|
|
|
}
|
|
|
|
|
2021-09-18 06:19:29 -05:00
|
|
|
let descended = sema.descend_node_into_attributes(pat.clone()).pop();
|
|
|
|
let desc_pat = descended.as_ref().unwrap_or(pat);
|
|
|
|
let ty = sema.type_of_pat(&desc_pat.clone().into())?.original;
|
2020-02-29 16:24:50 -06:00
|
|
|
|
2022-05-15 06:17:52 -05:00
|
|
|
if should_not_display_type_hint(sema, config, pat, &ty) {
|
2020-02-29 16:24:50 -06:00
|
|
|
return None;
|
|
|
|
}
|
2021-07-31 08:24:21 -05:00
|
|
|
|
2022-03-31 04:12:08 -05:00
|
|
|
let krate = sema.scope(desc_pat.syntax())?.krate();
|
2021-10-03 07:45:21 -05:00
|
|
|
let famous_defs = FamousDefs(sema, krate);
|
|
|
|
let label = hint_iterator(sema, &famous_defs, config, &ty);
|
|
|
|
|
|
|
|
let label = match label {
|
|
|
|
Some(label) => label,
|
|
|
|
None => {
|
2021-10-03 08:31:35 -05:00
|
|
|
let ty_name = ty.display_truncated(sema.db, config.max_length).to_string();
|
2021-11-13 17:12:29 -06:00
|
|
|
if config.hide_named_constructor_hints
|
|
|
|
&& is_named_constructor(sema, pat, &ty_name).is_some()
|
|
|
|
{
|
2021-10-03 07:45:21 -05:00
|
|
|
return None;
|
|
|
|
}
|
2022-05-17 05:56:14 -05:00
|
|
|
ty_name
|
2021-10-03 07:45:21 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-07 04:30:42 -05:00
|
|
|
acc.push(InlayHint {
|
2021-07-31 08:24:21 -05:00
|
|
|
range: match pat.name() {
|
2021-09-18 06:19:29 -05:00
|
|
|
Some(name) => name.syntax().text_range(),
|
|
|
|
None => pat.syntax().text_range(),
|
2021-07-31 08:24:21 -05:00
|
|
|
},
|
2020-10-07 04:30:42 -05:00
|
|
|
kind: InlayKind::TypeHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: label.into(),
|
2022-05-19 06:38:37 -05:00
|
|
|
tooltip: pat
|
|
|
|
.name()
|
|
|
|
.map(|it| it.syntax().text_range())
|
|
|
|
.map(|it| InlayTooltip::HoverRanged(file_id, it)),
|
2020-10-07 04:30:42 -05:00
|
|
|
});
|
2020-10-06 12:07:34 -05:00
|
|
|
|
2020-01-16 07:31:34 -06:00
|
|
|
Some(())
|
2019-07-21 15:28:05 -05:00
|
|
|
}
|
2020-01-16 07:31:34 -06:00
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
fn is_named_constructor(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-10-03 08:31:35 -05:00
|
|
|
pat: &ast::IdentPat,
|
|
|
|
ty_name: &str,
|
|
|
|
) -> Option<()> {
|
|
|
|
let let_node = pat.syntax().parent()?;
|
2021-10-03 07:45:21 -05:00
|
|
|
let expr = match_ast! {
|
2021-10-03 08:31:35 -05:00
|
|
|
match let_node {
|
2021-10-03 07:45:21 -05:00
|
|
|
ast::LetStmt(it) => it.initializer(),
|
2022-01-23 16:37:59 -06:00
|
|
|
ast::LetExpr(it) => it.expr(),
|
2021-10-03 07:45:21 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
2021-10-03 08:31:35 -05:00
|
|
|
}?;
|
|
|
|
|
|
|
|
let expr = sema.descend_node_into_attributes(expr.clone()).pop().unwrap_or(expr);
|
|
|
|
// unwrap postfix expressions
|
|
|
|
let expr = match expr {
|
|
|
|
ast::Expr::TryExpr(it) => it.expr(),
|
|
|
|
ast::Expr::AwaitExpr(it) => it.expr(),
|
|
|
|
expr => Some(expr),
|
|
|
|
}?;
|
|
|
|
let expr = match expr {
|
|
|
|
ast::Expr::CallExpr(call) => match call.expr()? {
|
2021-12-21 10:26:37 -06:00
|
|
|
ast::Expr::PathExpr(path) => path,
|
2021-10-03 08:31:35 -05:00
|
|
|
_ => return None,
|
|
|
|
},
|
2021-12-21 10:26:37 -06:00
|
|
|
ast::Expr::PathExpr(path) => path,
|
2021-10-03 08:31:35 -05:00
|
|
|
_ => return None,
|
2021-10-03 07:45:21 -05:00
|
|
|
};
|
2021-10-03 08:31:35 -05:00
|
|
|
let path = expr.path()?;
|
2021-10-03 07:45:21 -05:00
|
|
|
|
2021-12-22 09:29:26 -06:00
|
|
|
let callable = sema.type_of_expr(&ast::Expr::PathExpr(expr))?.original.as_callable(sema.db);
|
|
|
|
let callable_kind = callable.map(|it| it.kind());
|
|
|
|
let qual_seg = match callable_kind {
|
|
|
|
Some(hir::CallableKind::Function(_) | hir::CallableKind::TupleEnumVariant(_)) => {
|
|
|
|
path.qualifier()?.segment()
|
|
|
|
}
|
|
|
|
_ => path.segment(),
|
2021-12-21 10:26:37 -06:00
|
|
|
}?;
|
2021-12-22 09:29:26 -06:00
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
let ctor_name = match qual_seg.kind()? {
|
|
|
|
ast::PathSegmentKind::Name(name_ref) => {
|
|
|
|
match qual_seg.generic_arg_list().map(|it| it.generic_args()) {
|
|
|
|
Some(generics) => format!("{}<{}>", name_ref, generics.format(", ")),
|
|
|
|
None => name_ref.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::PathSegmentKind::Type { type_ref: Some(ty), trait_ref: None } => ty.to_string(),
|
|
|
|
_ => return None,
|
|
|
|
};
|
2021-10-16 06:32:55 -05:00
|
|
|
(ctor_name == ty_name).then(|| ())
|
2021-10-03 07:45:21 -05:00
|
|
|
}
|
|
|
|
|
2020-10-06 12:07:34 -05:00
|
|
|
/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
|
|
|
|
fn hint_iterator(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
|
|
|
famous_defs: &FamousDefs<'_, '_>,
|
2020-10-06 12:07:34 -05:00
|
|
|
config: &InlayHintsConfig,
|
2020-10-07 06:14:12 -05:00
|
|
|
ty: &hir::Type,
|
2022-05-17 05:56:14 -05:00
|
|
|
) -> Option<String> {
|
2020-10-06 14:05:57 -05:00
|
|
|
let db = sema.db;
|
2021-05-05 15:55:12 -05:00
|
|
|
let strukt = ty.strip_references().as_adt()?;
|
2021-05-23 18:42:06 -05:00
|
|
|
let krate = strukt.module(db).krate();
|
2020-10-20 10:38:21 -05:00
|
|
|
if krate != famous_defs.core()? {
|
2020-10-06 12:07:34 -05:00
|
|
|
return None;
|
|
|
|
}
|
2020-10-20 10:38:21 -05:00
|
|
|
let iter_trait = famous_defs.core_iter_Iterator()?;
|
|
|
|
let iter_mod = famous_defs.core_iter()?;
|
2021-05-23 11:50:23 -05:00
|
|
|
|
|
|
|
// Assert that this struct comes from `core::iter`.
|
2021-07-20 09:19:02 -05:00
|
|
|
if !(strukt.visibility(db) == hir::Visibility::Public
|
|
|
|
&& strukt.module(db).path_to_root(db).contains(&iter_mod))
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
2021-05-23 11:50:23 -05:00
|
|
|
|
2020-10-06 12:07:34 -05:00
|
|
|
if ty.impls_trait(db, iter_trait, &[]) {
|
|
|
|
let assoc_type_item = iter_trait.items(db).into_iter().find_map(|item| match item {
|
2020-10-07 06:14:12 -05:00
|
|
|
hir::AssocItem::TypeAlias(alias) if alias.name(db) == known::Item => Some(alias),
|
2020-10-06 12:07:34 -05:00
|
|
|
_ => None,
|
|
|
|
})?;
|
2021-04-03 14:50:52 -05:00
|
|
|
if let Some(ty) = ty.normalize_trait_assoc_type(db, &[], assoc_type_item) {
|
2020-10-06 14:05:57 -05:00
|
|
|
const LABEL_START: &str = "impl Iterator<Item = ";
|
|
|
|
const LABEL_END: &str = ">";
|
|
|
|
|
2020-10-20 10:38:21 -05:00
|
|
|
let ty_display = hint_iterator(sema, famous_defs, config, &ty)
|
2020-10-10 13:37:20 -05:00
|
|
|
.map(|assoc_type_impl| assoc_type_impl.to_string())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
ty.display_truncated(
|
|
|
|
db,
|
|
|
|
config
|
|
|
|
.max_length
|
|
|
|
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
|
|
|
|
)
|
|
|
|
.to_string()
|
|
|
|
});
|
2022-05-17 05:56:14 -05:00
|
|
|
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END));
|
2020-10-06 12:07:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-10-07 06:14:12 -05:00
|
|
|
fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir::Type) -> bool {
|
|
|
|
if let Some(hir::Adt::Enum(enum_data)) = pat_ty.as_adt() {
|
2020-05-03 07:07:57 -05:00
|
|
|
let pat_text = bind_pat.to_string();
|
2020-02-24 01:29:34 -06:00
|
|
|
enum_data
|
|
|
|
.variants(db)
|
|
|
|
.into_iter()
|
2021-11-04 12:12:05 -05:00
|
|
|
.map(|variant| variant.name(db).to_smol_str())
|
2020-02-24 01:29:34 -06:00
|
|
|
.any(|enum_name| enum_name == pat_text)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:09:09 -05:00
|
|
|
fn should_not_display_type_hint(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2022-05-15 06:17:52 -05:00
|
|
|
config: &InlayHintsConfig,
|
2020-07-31 13:09:09 -05:00
|
|
|
bind_pat: &ast::IdentPat,
|
2020-10-07 06:14:12 -05:00
|
|
|
pat_ty: &hir::Type,
|
2020-07-31 13:09:09 -05:00
|
|
|
) -> bool {
|
2020-09-13 12:24:04 -05:00
|
|
|
let db = sema.db;
|
|
|
|
|
2020-02-24 01:29:34 -06:00
|
|
|
if pat_ty.is_unknown() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-07 06:14:12 -05:00
|
|
|
if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() {
|
2021-11-04 12:12:05 -05:00
|
|
|
if s.fields(db).is_empty() && s.name(db).to_smol_str() == bind_pat.to_string() {
|
2020-03-08 16:21:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-15 06:17:52 -05:00
|
|
|
if config.hide_closure_initialization_hints {
|
|
|
|
if let Some(parent) = bind_pat.syntax().parent() {
|
|
|
|
if let Some(it) = ast::LetStmt::cast(parent.clone()) {
|
|
|
|
if let Some(ast::Expr::ClosureExpr(closure)) = it.initializer() {
|
|
|
|
if closure_has_block_body(&closure) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 14:07:09 -06:00
|
|
|
for node in bind_pat.syntax().ancestors() {
|
|
|
|
match_ast! {
|
|
|
|
match node {
|
2021-06-03 09:22:24 -05:00
|
|
|
ast::LetStmt(it) => return it.ty().is_some(),
|
2022-05-14 07:26:08 -05:00
|
|
|
// FIXME: We might wanna show type hints in parameters for non-top level patterns as well
|
2021-06-03 09:22:24 -05:00
|
|
|
ast::Param(it) => return it.ty().is_some(),
|
2022-01-23 16:37:59 -06:00
|
|
|
ast::MatchArm(_) => return pat_is_enum_variant(db, bind_pat, pat_ty),
|
|
|
|
ast::LetExpr(_) => return pat_is_enum_variant(db, bind_pat, pat_ty),
|
|
|
|
ast::IfExpr(_) => return false,
|
|
|
|
ast::WhileExpr(_) => return false,
|
2020-09-13 12:24:04 -05:00
|
|
|
ast::ForExpr(it) => {
|
|
|
|
// We *should* display hint only if user provided "in {expr}" and we know the type of expr (and it's not unit).
|
|
|
|
// Type of expr should be iterable.
|
2020-10-03 05:44:43 -05:00
|
|
|
return it.in_token().is_none() ||
|
|
|
|
it.iterable()
|
2021-06-04 06:47:39 -05:00
|
|
|
.and_then(|iterable_expr| sema.type_of_expr(&iterable_expr))
|
2021-08-03 10:28:51 -05:00
|
|
|
.map(TypeInfo::original)
|
2021-06-04 06:47:39 -05:00
|
|
|
.map_or(true, |iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
|
2020-09-13 12:24:04 -05:00
|
|
|
},
|
2020-02-22 14:07:09 -06:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-05-15 06:17:52 -05:00
|
|
|
fn closure_has_block_body(closure: &ast::ClosureExpr) -> bool {
|
|
|
|
matches!(closure.body(), Some(ast::Expr::BlockExpr(_)))
|
|
|
|
}
|
|
|
|
|
2021-06-03 09:22:24 -05:00
|
|
|
fn should_hide_param_name_hint(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-10-07 06:14:12 -05:00
|
|
|
callable: &hir::Callable,
|
2020-02-23 03:49:53 -06:00
|
|
|
param_name: &str,
|
2020-02-24 01:29:34 -06:00
|
|
|
argument: &ast::Expr,
|
2020-02-23 03:49:53 -06:00
|
|
|
) -> bool {
|
2021-06-04 07:09:20 -05:00
|
|
|
// These are to be tested in the `parameter_hint_heuristics` test
|
2021-06-03 09:22:24 -05:00
|
|
|
// hide when:
|
|
|
|
// - the parameter name is a suffix of the function's name
|
2022-05-19 08:33:04 -05:00
|
|
|
// - the argument is a qualified constructing or call expression where the qualifier is an ADT
|
2021-06-04 06:47:39 -05:00
|
|
|
// - exact argument<->parameter match(ignoring leading underscore) or parameter is a prefix/suffix
|
|
|
|
// of argument with _ splitting it off
|
2021-06-03 09:22:24 -05:00
|
|
|
// - param starts with `ra_fixture`
|
2021-08-22 10:48:15 -05:00
|
|
|
// - param is a well known name in a unary function
|
2021-06-03 09:22:24 -05:00
|
|
|
|
2020-05-03 07:50:35 -05:00
|
|
|
let param_name = param_name.trim_start_matches('_');
|
2021-06-03 09:22:24 -05:00
|
|
|
if param_name.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:12:47 -06:00
|
|
|
if matches!(argument, ast::Expr::PrefixExpr(prefix) if prefix.op_kind() == Some(UnaryOp::Not)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-16 14:51:44 -05:00
|
|
|
let fn_name = match callable.kind() {
|
2021-11-04 12:12:05 -05:00
|
|
|
hir::CallableKind::Function(it) => Some(it.name(sema.db).to_smol_str()),
|
2021-06-03 09:22:24 -05:00
|
|
|
_ => None,
|
2020-07-16 14:51:44 -05:00
|
|
|
};
|
2021-06-03 09:22:24 -05:00
|
|
|
let fn_name = fn_name.as_deref();
|
|
|
|
is_param_name_suffix_of_fn_name(param_name, callable, fn_name)
|
|
|
|
|| is_argument_similar_to_param_name(argument, param_name)
|
2020-06-28 06:11:41 -05:00
|
|
|
|| param_name.starts_with("ra_fixture")
|
2021-06-03 09:22:24 -05:00
|
|
|
|| (callable.n_params() == 1 && is_obvious_param(param_name))
|
2022-05-19 08:33:04 -05:00
|
|
|
|| is_adt_constructor_similar_to_param_name(sema, argument, param_name)
|
2020-04-09 09:35:07 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:22:24 -05:00
|
|
|
fn is_argument_similar_to_param_name(argument: &ast::Expr, param_name: &str) -> bool {
|
2021-06-04 06:47:39 -05:00
|
|
|
// check whether param_name and argument are the same or
|
|
|
|
// whether param_name is a prefix/suffix of argument(split at `_`)
|
|
|
|
let argument = match get_string_representation(argument) {
|
|
|
|
Some(argument) => argument,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
2021-11-19 05:46:03 -06:00
|
|
|
// std is honestly too panic happy...
|
|
|
|
let str_split_at = |str: &str, at| str.is_char_boundary(at).then(|| argument.split_at(at));
|
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
let param_name = param_name.trim_start_matches('_');
|
|
|
|
let argument = argument.trim_start_matches('_');
|
2021-11-19 05:46:03 -06:00
|
|
|
|
|
|
|
match str_split_at(argument, param_name.len()) {
|
|
|
|
Some((prefix, rest)) if prefix.eq_ignore_ascii_case(param_name) => {
|
|
|
|
return rest.is_empty() || rest.starts_with('_');
|
|
|
|
}
|
|
|
|
_ => (),
|
2021-06-04 06:47:39 -05:00
|
|
|
}
|
2021-11-19 05:46:03 -06:00
|
|
|
match argument.len().checked_sub(param_name.len()).and_then(|at| str_split_at(argument, at)) {
|
|
|
|
Some((rest, suffix)) if param_name.eq_ignore_ascii_case(suffix) => {
|
|
|
|
return rest.is_empty() || rest.ends_with('_');
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-05-03 07:50:35 -05:00
|
|
|
}
|
2021-11-19 05:46:03 -06:00
|
|
|
false
|
2020-05-03 07:50:35 -05:00
|
|
|
}
|
|
|
|
|
2021-08-22 10:48:15 -05:00
|
|
|
/// Hide the parameter name of a unary function if it is a `_` - prefixed suffix of the function's name, or equal.
|
2021-06-03 09:22:24 -05:00
|
|
|
///
|
|
|
|
/// `fn strip_suffix(suffix)` will be hidden.
|
|
|
|
/// `fn stripsuffix(suffix)` will not be hidden.
|
|
|
|
fn is_param_name_suffix_of_fn_name(
|
2020-10-01 13:05:39 -05:00
|
|
|
param_name: &str,
|
|
|
|
callable: &Callable,
|
2021-06-03 09:22:24 -05:00
|
|
|
fn_name: Option<&str>,
|
2020-10-01 13:05:39 -05:00
|
|
|
) -> bool {
|
|
|
|
match (callable.n_params(), fn_name) {
|
|
|
|
(1, Some(function)) => {
|
|
|
|
function == param_name
|
2021-11-19 05:46:03 -06:00
|
|
|
|| function
|
|
|
|
.len()
|
|
|
|
.checked_sub(param_name.len())
|
|
|
|
.and_then(|at| function.is_char_boundary(at).then(|| function.split_at(at)))
|
|
|
|
.map_or(false, |(prefix, suffix)| {
|
|
|
|
suffix.eq_ignore_ascii_case(param_name) && prefix.ends_with('_')
|
|
|
|
})
|
2020-10-01 13:05:39 -05:00
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:33:04 -05:00
|
|
|
fn is_adt_constructor_similar_to_param_name(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2020-05-03 07:50:35 -05:00
|
|
|
argument: &ast::Expr,
|
|
|
|
param_name: &str,
|
|
|
|
) -> bool {
|
2022-05-19 08:33:04 -05:00
|
|
|
let path = match argument {
|
|
|
|
ast::Expr::CallExpr(c) => c.expr().and_then(|e| match e {
|
|
|
|
ast::Expr::PathExpr(p) => p.path(),
|
|
|
|
_ => None,
|
|
|
|
}),
|
|
|
|
ast::Expr::PathExpr(p) => p.path(),
|
|
|
|
ast::Expr::RecordExpr(r) => r.path(),
|
|
|
|
_ => return false,
|
|
|
|
};
|
|
|
|
let path = match path {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
(|| match sema.resolve_path(&path)? {
|
|
|
|
hir::PathResolution::Def(hir::ModuleDef::Adt(_)) => {
|
|
|
|
Some(to_lower_snake_case(&path.segment()?.name_ref()?.text()) == param_name)
|
2021-11-04 12:12:05 -05:00
|
|
|
}
|
2022-05-19 08:33:04 -05:00
|
|
|
hir::PathResolution::Def(hir::ModuleDef::Function(_) | hir::ModuleDef::Variant(_)) => {
|
|
|
|
if to_lower_snake_case(&path.segment()?.name_ref()?.text()) == param_name {
|
|
|
|
return Some(true);
|
|
|
|
}
|
|
|
|
let qual = path.qualifier()?;
|
|
|
|
match sema.resolve_path(&qual)? {
|
|
|
|
hir::PathResolution::Def(hir::ModuleDef::Adt(_)) => {
|
|
|
|
Some(to_lower_snake_case(&qual.segment()?.name_ref()?.text()) == param_name)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})()
|
|
|
|
.unwrap_or(false)
|
2020-05-03 07:50:35 -05:00
|
|
|
}
|
|
|
|
|
2020-05-03 07:07:57 -05:00
|
|
|
fn get_string_representation(expr: &ast::Expr) -> Option<String> {
|
|
|
|
match expr {
|
|
|
|
ast::Expr::MethodCallExpr(method_call_expr) => {
|
2020-10-22 12:42:39 -05:00
|
|
|
let name_ref = method_call_expr.name_ref()?;
|
2021-03-26 12:30:59 -05:00
|
|
|
match name_ref.text().as_str() {
|
2021-06-04 07:09:20 -05:00
|
|
|
"clone" | "as_ref" => method_call_expr.receiver().map(|rec| rec.to_string()),
|
2020-10-22 12:42:39 -05:00
|
|
|
name_ref => Some(name_ref.to_owned()),
|
|
|
|
}
|
2020-04-09 11:26:49 -05:00
|
|
|
}
|
2022-06-07 18:29:25 -05:00
|
|
|
ast::Expr::MacroExpr(macro_expr) => {
|
|
|
|
Some(macro_expr.macro_call()?.path()?.segment()?.to_string())
|
|
|
|
}
|
2021-01-27 07:20:58 -06:00
|
|
|
ast::Expr::FieldExpr(field_expr) => Some(field_expr.name_ref()?.to_string()),
|
2021-06-03 09:22:24 -05:00
|
|
|
ast::Expr::PathExpr(path_expr) => Some(path_expr.path()?.segment()?.to_string()),
|
2021-01-27 07:20:58 -06:00
|
|
|
ast::Expr::PrefixExpr(prefix_expr) => get_string_representation(&prefix_expr.expr()?),
|
2020-05-03 07:07:57 -05:00
|
|
|
ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?),
|
2022-05-10 06:43:43 -05:00
|
|
|
ast::Expr::CastExpr(cast_expr) => get_string_representation(&cast_expr.expr()?),
|
2021-01-27 07:20:58 -06:00
|
|
|
_ => None,
|
2020-04-09 11:26:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 09:35:07 -05:00
|
|
|
fn is_obvious_param(param_name: &str) -> bool {
|
2021-06-03 09:22:24 -05:00
|
|
|
// avoid displaying hints for common functions like map, filter, etc.
|
|
|
|
// or other obvious words used in std
|
2020-06-27 20:02:03 -05:00
|
|
|
let is_obvious_param_name =
|
|
|
|
matches!(param_name, "predicate" | "value" | "pat" | "rhs" | "other");
|
2020-04-09 09:35:07 -05:00
|
|
|
param_name.len() == 1 || is_obvious_param_name
|
2020-02-23 03:49:53 -06:00
|
|
|
}
|
|
|
|
|
2021-06-03 09:22:24 -05:00
|
|
|
fn get_callable(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-06-03 09:22:24 -05:00
|
|
|
expr: &ast::Expr,
|
|
|
|
) -> Option<(hir::Callable, ast::ArgList)> {
|
2020-01-14 11:02:01 -06:00
|
|
|
match expr {
|
2021-06-03 09:22:24 -05:00
|
|
|
ast::Expr::CallExpr(expr) => {
|
2021-09-18 06:19:29 -05:00
|
|
|
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
|
|
|
|
let expr = descended.as_ref().unwrap_or(expr);
|
2021-08-03 10:28:51 -05:00
|
|
|
sema.type_of_expr(&expr.expr()?)?.original.as_callable(sema.db).zip(expr.arg_list())
|
2021-06-03 09:22:24 -05:00
|
|
|
}
|
|
|
|
ast::Expr::MethodCallExpr(expr) => {
|
2021-09-18 06:19:29 -05:00
|
|
|
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
|
|
|
|
let expr = descended.as_ref().unwrap_or(expr);
|
2021-06-03 09:22:24 -05:00
|
|
|
sema.resolve_method_call_as_callable(expr).zip(expr.arg_list())
|
|
|
|
}
|
2020-01-14 11:02:01 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 15:28:05 -05:00
|
|
|
|
2019-07-19 16:20:09 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 06:19:31 -05:00
|
|
|
use expect_test::{expect, Expect};
|
2022-03-18 12:11:16 -05:00
|
|
|
use itertools::Itertools;
|
2022-02-11 16:48:01 -06:00
|
|
|
use syntax::{TextRange, TextSize};
|
2020-06-30 11:04:25 -05:00
|
|
|
use test_utils::extract_annotations;
|
2019-07-19 16:20:09 -05:00
|
|
|
|
2022-11-04 11:11:15 -05:00
|
|
|
use crate::inlay_hints::AdjustmentHints;
|
2022-03-19 13:01:19 -05:00
|
|
|
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
|
2020-06-30 11:04:25 -05:00
|
|
|
|
2022-05-28 07:13:25 -05:00
|
|
|
use super::ClosureReturnTypeHints;
|
|
|
|
|
2022-03-16 15:16:55 -05:00
|
|
|
const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
|
|
|
|
render_colons: false,
|
|
|
|
type_hints: false,
|
|
|
|
parameter_hints: false,
|
|
|
|
chaining_hints: false,
|
2022-03-19 13:01:19 -05:00
|
|
|
lifetime_elision_hints: LifetimeElisionHints::Never,
|
2022-05-28 07:13:25 -05:00
|
|
|
closure_return_type_hints: ClosureReturnTypeHints::Never,
|
2022-11-07 05:49:52 -06:00
|
|
|
adjustment_hints: AdjustmentHints::Never,
|
2022-05-14 07:26:08 -05:00
|
|
|
binding_mode_hints: false,
|
|
|
|
hide_named_constructor_hints: false,
|
2022-05-15 06:17:52 -05:00
|
|
|
hide_closure_initialization_hints: false,
|
2022-03-19 12:11:56 -05:00
|
|
|
param_names_for_lifetime_elision_hints: false,
|
2022-03-16 15:16:55 -05:00
|
|
|
max_length: None,
|
2022-05-13 12:42:59 -05:00
|
|
|
closing_brace_hints_min_lines: None,
|
2022-03-16 15:16:55 -05:00
|
|
|
};
|
2021-01-06 04:54:28 -06:00
|
|
|
const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig {
|
|
|
|
type_hints: true,
|
|
|
|
parameter_hints: true,
|
|
|
|
chaining_hints: true,
|
2022-05-28 07:13:25 -05:00
|
|
|
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
|
2022-05-14 07:26:08 -05:00
|
|
|
binding_mode_hints: true,
|
2022-03-19 13:01:19 -05:00
|
|
|
lifetime_elision_hints: LifetimeElisionHints::Always,
|
2022-03-16 15:16:55 -05:00
|
|
|
..DISABLED_CONFIG
|
2021-01-06 04:54:28 -06:00
|
|
|
};
|
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2020-06-30 11:04:25 -05:00
|
|
|
fn check(ra_fixture: &str) {
|
2021-01-06 04:54:28 -06:00
|
|
|
check_with_config(TEST_CONFIG, ra_fixture);
|
2020-06-30 11:04:25 -05:00
|
|
|
}
|
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn check_params(ra_fixture: &str) {
|
|
|
|
check_with_config(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { parameter_hints: true, ..DISABLED_CONFIG },
|
2021-06-04 06:47:39 -05:00
|
|
|
ra_fixture,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn check_types(ra_fixture: &str) {
|
2022-03-16 15:16:55 -05:00
|
|
|
check_with_config(InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG }, ra_fixture);
|
2021-06-04 06:47:39 -05:00
|
|
|
}
|
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn check_chains(ra_fixture: &str) {
|
2022-03-16 15:16:55 -05:00
|
|
|
check_with_config(InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG }, ra_fixture);
|
2021-06-04 06:47:39 -05:00
|
|
|
}
|
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2020-07-09 09:12:53 -05:00
|
|
|
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
|
2021-10-16 06:32:55 -05:00
|
|
|
let (analysis, file_id) = fixture::file(ra_fixture);
|
2022-03-18 12:11:16 -05:00
|
|
|
let mut expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
|
2022-02-11 16:48:01 -06:00
|
|
|
let inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();
|
2022-03-18 12:11:16 -05:00
|
|
|
let actual = inlay_hints
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| (it.range, it.label.to_string()))
|
|
|
|
.sorted_by_key(|(range, _)| range.start())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
expected.sort_by_key(|(range, _)| range.start());
|
|
|
|
|
2020-07-16 14:51:44 -05:00
|
|
|
assert_eq!(expected, actual, "\nExpected:\n{:#?}\n\nActual:\n{:#?}", expected, actual);
|
2020-06-30 11:04:25 -05:00
|
|
|
}
|
2019-12-07 12:14:01 -06:00
|
|
|
|
2021-10-03 08:31:35 -05:00
|
|
|
#[track_caller]
|
2020-07-09 09:12:53 -05:00
|
|
|
fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) {
|
2021-10-16 06:32:55 -05:00
|
|
|
let (analysis, file_id) = fixture::file(ra_fixture);
|
2022-02-11 16:48:01 -06:00
|
|
|
let inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();
|
2020-06-30 14:55:21 -05:00
|
|
|
expect.assert_debug_eq(&inlay_hints)
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:55:46 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn hints_disabled() {
|
2020-06-30 11:04:25 -05:00
|
|
|
check_with_config(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { render_colons: true, ..DISABLED_CONFIG },
|
2020-03-10 02:55:46 -05:00
|
|
|
r#"
|
2020-06-30 11:04:25 -05:00
|
|
|
fn foo(a: i32, b: i32) -> i32 { a + b }
|
2021-06-04 06:47:39 -05:00
|
|
|
fn main() {
|
|
|
|
let _x = foo(4, 4);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
// Parameter hint tests
|
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
#[test]
|
|
|
|
fn param_hints_only() {
|
|
|
|
check_params(
|
|
|
|
r#"
|
|
|
|
fn foo(a: i32, b: i32) -> i32 { a + b }
|
2020-06-30 11:04:25 -05:00
|
|
|
fn main() {
|
|
|
|
let _x = foo(
|
|
|
|
4,
|
|
|
|
//^ a
|
|
|
|
4,
|
|
|
|
//^ b
|
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
2020-03-10 02:55:46 -05:00
|
|
|
}
|
|
|
|
|
2022-05-19 11:53:08 -05:00
|
|
|
#[test]
|
|
|
|
fn param_hints_on_closure() {
|
|
|
|
check_params(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let clo = |a: u8, b: u8| a + b;
|
|
|
|
clo(
|
|
|
|
1,
|
|
|
|
//^ a
|
|
|
|
2,
|
|
|
|
//^ b
|
|
|
|
);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:05:39 -05:00
|
|
|
#[test]
|
|
|
|
fn param_name_similar_to_fn_name_still_hints() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_params(
|
2020-10-01 13:05:39 -05:00
|
|
|
r#"
|
|
|
|
fn max(x: i32, y: i32) -> i32 { x + y }
|
|
|
|
fn main() {
|
|
|
|
let _x = max(
|
|
|
|
4,
|
|
|
|
//^ x
|
|
|
|
4,
|
|
|
|
//^ y
|
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn param_name_similar_to_fn_name() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_params(
|
2020-10-01 13:05:39 -05:00
|
|
|
r#"
|
|
|
|
fn param_with_underscore(with_underscore: i32) -> i32 { with_underscore }
|
2021-06-04 07:09:20 -05:00
|
|
|
fn main() {
|
|
|
|
let _x = param_with_underscore(
|
|
|
|
4,
|
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
check_params(
|
|
|
|
r#"
|
|
|
|
fn param_with_underscore(underscore: i32) -> i32 { underscore }
|
2020-10-01 13:05:39 -05:00
|
|
|
fn main() {
|
|
|
|
let _x = param_with_underscore(
|
|
|
|
4,
|
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn param_name_same_as_fn_name() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_params(
|
2020-10-01 13:05:39 -05:00
|
|
|
r#"
|
|
|
|
fn foo(foo: i32) -> i32 { foo }
|
|
|
|
fn main() {
|
|
|
|
let _x = foo(
|
|
|
|
4,
|
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn never_hide_param_when_multiple_params() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_params(
|
2020-10-01 13:05:39 -05:00
|
|
|
r#"
|
2021-06-04 07:09:20 -05:00
|
|
|
fn foo(foo: i32, bar: i32) -> i32 { bar + baz }
|
2020-10-01 13:05:39 -05:00
|
|
|
fn main() {
|
|
|
|
let _x = foo(
|
|
|
|
4,
|
2021-06-04 07:09:20 -05:00
|
|
|
//^ foo
|
2020-10-01 13:05:39 -05:00
|
|
|
8,
|
2021-06-04 07:09:20 -05:00
|
|
|
//^ bar
|
2020-10-01 13:05:39 -05:00
|
|
|
);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:55:46 -05:00
|
|
|
#[test]
|
2021-06-04 07:09:20 -05:00
|
|
|
fn param_hints_look_through_as_ref_and_clone() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_params(
|
2020-07-09 09:12:53 -05:00
|
|
|
r#"
|
2021-06-04 07:09:20 -05:00
|
|
|
fn foo(bar: i32, baz: f32) {}
|
2021-06-04 06:47:39 -05:00
|
|
|
|
2020-07-09 09:12:53 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let bar = 3;
|
|
|
|
let baz = &"baz";
|
|
|
|
let fez = 1.0;
|
2021-06-04 07:09:20 -05:00
|
|
|
foo(bar.clone(), bar.clone());
|
|
|
|
//^^^^^^^^^^^ baz
|
|
|
|
foo(bar.as_ref(), bar.as_ref());
|
|
|
|
//^^^^^^^^^^^^ baz
|
2021-06-04 06:47:39 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:55:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn self_param_hints() {
|
|
|
|
check_params(
|
2020-07-09 09:12:53 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self: Self) {}
|
|
|
|
fn bar(self: &Self) {}
|
|
|
|
}
|
|
|
|
|
2020-07-09 09:12:53 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
Foo::foo(Foo);
|
|
|
|
//^^^ self
|
|
|
|
Foo::bar(&Foo);
|
|
|
|
//^^^^ self
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
2020-03-10 02:55:46 -05:00
|
|
|
}
|
2020-06-30 11:04:25 -05:00
|
|
|
|
2019-12-07 16:54:18 -06:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn param_name_hints_show_for_literals() {
|
|
|
|
check_params(
|
|
|
|
r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] }
|
2019-12-07 16:54:18 -06:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
test(
|
2021-06-04 07:09:20 -05:00
|
|
|
0xa_b,
|
|
|
|
//^^^^^ a
|
|
|
|
0xa_b,
|
|
|
|
//^^^^^ b
|
2021-06-04 06:47:39 -05:00
|
|
|
);
|
2019-12-07 16:54:18 -06:00
|
|
|
}"#,
|
2021-06-04 06:47:39 -05:00
|
|
|
)
|
2019-12-07 16:54:18 -06:00
|
|
|
}
|
|
|
|
|
2019-07-19 16:20:09 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn function_call_parameter_hint() {
|
2021-06-04 07:09:20 -05:00
|
|
|
check_params(
|
2019-07-19 16:20:09 -05:00
|
|
|
r#"
|
2021-06-18 15:38:19 -05:00
|
|
|
//- minicore: option
|
2021-06-04 06:47:39 -05:00
|
|
|
struct FileId {}
|
|
|
|
struct SmolStr {}
|
2019-07-19 16:20:09 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
struct TextRange {}
|
|
|
|
struct SyntaxKind {}
|
|
|
|
struct NavigationTarget {}
|
2019-07-19 16:20:09 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Test {}
|
2019-07-26 06:10:29 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
impl Test {
|
|
|
|
fn method(&self, mut param: i32) -> i32 { param * 2 }
|
2019-07-26 06:10:29 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
fn from_syntax(
|
|
|
|
file_id: FileId,
|
|
|
|
name: SmolStr,
|
|
|
|
focus_range: Option<TextRange>,
|
|
|
|
full_range: TextRange,
|
|
|
|
kind: SyntaxKind,
|
|
|
|
docs: Option<String>,
|
|
|
|
) -> NavigationTarget {
|
|
|
|
NavigationTarget {}
|
2019-07-27 16:50:26 -05:00
|
|
|
}
|
2021-06-04 06:47:39 -05:00
|
|
|
}
|
2019-07-27 16:50:26 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
fn test_func(mut foo: i32, bar: i32, msg: &str, _: i32, last: i32) -> i32 {
|
|
|
|
foo + bar
|
|
|
|
}
|
2019-12-23 09:53:35 -06:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
fn main() {
|
|
|
|
let not_literal = 1;
|
|
|
|
let _: i32 = test_func(1, 2, "hello", 3, not_literal);
|
|
|
|
//^ foo ^ bar ^^^^^^^ msg ^^^^^^^^^^^ last
|
|
|
|
let t: Test = Test {};
|
|
|
|
t.method(123);
|
|
|
|
//^^^ param
|
|
|
|
Test::method(&t, 3456);
|
|
|
|
//^^ self ^^^^ param
|
|
|
|
Test::from_syntax(
|
|
|
|
FileId {},
|
|
|
|
"impl".into(),
|
|
|
|
//^^^^^^^^^^^^^ name
|
|
|
|
None,
|
|
|
|
//^^^^ focus_range
|
|
|
|
TextRange {},
|
|
|
|
//^^^^^^^^^^^^ full_range
|
|
|
|
SyntaxKind {},
|
|
|
|
//^^^^^^^^^^^^^ kind
|
|
|
|
None,
|
|
|
|
//^^^^ docs
|
|
|
|
);
|
2019-07-27 16:50:26 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-04 07:09:20 -05:00
|
|
|
fn parameter_hint_heuristics() {
|
|
|
|
check_params(
|
2019-07-27 16:50:26 -05:00
|
|
|
r#"
|
2021-06-04 07:09:20 -05:00
|
|
|
fn check(ra_fixture_thing: &str) {}
|
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
fn map(f: i32) {}
|
|
|
|
fn filter(predicate: i32) {}
|
2019-07-27 16:50:26 -05:00
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
fn strip_suffix(suffix: &str) {}
|
|
|
|
fn stripsuffix(suffix: &str) {}
|
|
|
|
fn same(same: u32) {}
|
|
|
|
fn same2(_same2: u32) {}
|
2020-04-09 05:47:48 -05:00
|
|
|
|
2020-05-03 07:50:35 -05:00
|
|
|
fn enum_matches_param_name(completion_kind: CompletionKind) {}
|
2020-04-08 16:48:16 -05:00
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
fn foo(param: u32) {}
|
|
|
|
fn bar(param_eter: u32) {}
|
2020-04-09 05:47:48 -05:00
|
|
|
|
2020-05-03 07:50:35 -05:00
|
|
|
enum CompletionKind {
|
|
|
|
Keyword,
|
|
|
|
}
|
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
fn non_ident_pat((a, b): (u32, u32)) {}
|
2020-02-23 03:49:53 -06:00
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
fn main() {
|
2021-11-19 05:46:03 -06:00
|
|
|
const PARAM: u32 = 0;
|
|
|
|
foo(PARAM);
|
2021-11-27 12:12:47 -06:00
|
|
|
foo(!PARAM);
|
|
|
|
// ^^^^^^ param
|
2021-06-04 07:09:20 -05:00
|
|
|
check("");
|
2021-01-09 02:33:28 -06:00
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
map(0);
|
|
|
|
filter(0);
|
2020-04-08 16:48:16 -05:00
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
strip_suffix("");
|
|
|
|
stripsuffix("");
|
|
|
|
//^^ suffix
|
|
|
|
same(0);
|
|
|
|
same2(0);
|
2020-04-18 02:53:48 -05:00
|
|
|
|
2020-05-03 07:50:35 -05:00
|
|
|
enum_matches_param_name(CompletionKind::Keyword);
|
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
let param = 0;
|
|
|
|
foo(param);
|
2022-05-10 06:43:43 -05:00
|
|
|
foo(param as _);
|
2021-06-04 07:09:20 -05:00
|
|
|
let param_end = 0;
|
|
|
|
foo(param_end);
|
|
|
|
let start_param = 0;
|
|
|
|
foo(start_param);
|
|
|
|
let param2 = 0;
|
|
|
|
foo(param2);
|
|
|
|
//^^^^^^ param
|
|
|
|
|
2022-06-07 18:29:25 -05:00
|
|
|
macro_rules! param {
|
|
|
|
() => {};
|
|
|
|
};
|
|
|
|
foo(param!());
|
|
|
|
|
2021-06-04 07:09:20 -05:00
|
|
|
let param_eter = 0;
|
|
|
|
bar(param_eter);
|
|
|
|
let param_eter_end = 0;
|
|
|
|
bar(param_eter_end);
|
|
|
|
let start_param_eter = 0;
|
|
|
|
bar(start_param_eter);
|
|
|
|
let param_eter2 = 0;
|
|
|
|
bar(param_eter2);
|
|
|
|
//^^^^^^^^^^^ param_eter
|
|
|
|
|
|
|
|
non_ident_pat((0, 0));
|
2020-02-23 03:49:53 -06:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2020-03-08 16:21:08 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
// Type-Hint tests
|
|
|
|
|
2020-03-08 16:21:08 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn type_hints_only() {
|
|
|
|
check_types(
|
2020-03-08 16:21:08 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
fn foo(a: i32, b: i32) -> i32 { a + b }
|
2020-03-08 16:21:08 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let _x = foo(4, 4);
|
|
|
|
//^^ i32
|
2020-03-08 16:21:08 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2020-03-24 13:33:00 -05:00
|
|
|
|
2021-07-31 08:24:21 -05:00
|
|
|
#[test]
|
|
|
|
fn type_hints_bindings_after_at() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
|
|
|
//- minicore: option
|
|
|
|
fn main() {
|
|
|
|
let ref foo @ bar @ ref mut baz = 0;
|
|
|
|
//^^^ &i32
|
|
|
|
//^^^ i32
|
|
|
|
//^^^ &mut i32
|
|
|
|
let [x @ ..] = [0];
|
|
|
|
//^ [i32; 1]
|
|
|
|
if let x @ Some(_) = Some(0) {}
|
|
|
|
//^ Option<i32>
|
|
|
|
let foo @ (bar, baz) = (3, 3);
|
|
|
|
//^^^ (i32, i32)
|
|
|
|
//^^^ i32
|
|
|
|
//^^^ i32
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:33:00 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn default_generic_types_should_not_be_displayed() {
|
|
|
|
check(
|
2020-03-24 13:33:00 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Test<K, T = u8> { k: K, t: T }
|
2020-06-30 14:55:21 -05:00
|
|
|
|
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let zz = Test { t: 23u8, k: 33 };
|
|
|
|
//^^ Test<i32>
|
|
|
|
let zz_ref = &zz;
|
|
|
|
//^^^^^^ &Test<i32>
|
|
|
|
let test = || zz;
|
|
|
|
//^^^^ || -> Test<i32>
|
|
|
|
}"#,
|
2020-06-30 14:55:21 -05:00
|
|
|
);
|
2020-03-24 13:33:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn shorten_iterators_in_associated_params() {
|
|
|
|
check_types(
|
2020-03-24 13:33:00 -05:00
|
|
|
r#"
|
2021-06-17 03:28:44 -05:00
|
|
|
//- minicore: iterators
|
2021-06-04 06:47:39 -05:00
|
|
|
use core::iter;
|
|
|
|
|
|
|
|
pub struct SomeIter<T> {}
|
|
|
|
|
|
|
|
impl<T> SomeIter<T> {
|
|
|
|
pub fn new() -> Self { SomeIter {} }
|
|
|
|
pub fn push(&mut self, t: T) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Iterator for SomeIter<T> {
|
|
|
|
type Item = T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 13:33:00 -05:00
|
|
|
|
2020-06-30 11:04:25 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let mut some_iter = SomeIter::new();
|
2021-07-31 08:24:21 -05:00
|
|
|
//^^^^^^^^^ SomeIter<Take<Repeat<i32>>>
|
2021-06-04 06:47:39 -05:00
|
|
|
some_iter.push(iter::repeat(2).take(2));
|
|
|
|
let iter_of_iters = some_iter.take(2);
|
|
|
|
//^^^^^^^^^^^^^ impl Iterator<Item = impl Iterator<Item = i32>>
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-24 13:33:00 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-12 07:56:46 -05:00
|
|
|
#[test]
|
|
|
|
fn iterator_hint_regression_issue_12674() {
|
|
|
|
// Ensure we don't crash while solving the projection type of iterators.
|
|
|
|
check_expect(
|
|
|
|
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
|
|
|
|
r#"
|
|
|
|
//- minicore: iterators
|
|
|
|
struct S<T>(T);
|
|
|
|
impl<T> S<T> {
|
|
|
|
fn iter(&self) -> Iter<'_, T> { loop {} }
|
|
|
|
}
|
|
|
|
struct Iter<'a, T: 'a>(&'a T);
|
|
|
|
impl<'a, T> Iterator for Iter<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> { loop {} }
|
|
|
|
}
|
|
|
|
struct Container<'a> {
|
|
|
|
elements: S<&'a str>,
|
|
|
|
}
|
|
|
|
struct SliceIter<'a, T>(&'a T);
|
|
|
|
impl<'a, T> Iterator for SliceIter<'a, T> {
|
|
|
|
type Item = &'a T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> { loop {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main(a: SliceIter<'_, Container>) {
|
|
|
|
a
|
|
|
|
.filter_map(|c| Some(c.elements.iter().filter_map(|v| Some(v))))
|
|
|
|
.map(|e| e);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
|
|
|
range: 484..554,
|
|
|
|
kind: ChainingHint,
|
|
|
|
label: [
|
|
|
|
"impl Iterator<Item = impl Iterator<Item = &&str>>",
|
|
|
|
],
|
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
484..554,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: 484..485,
|
|
|
|
kind: ChainingHint,
|
|
|
|
label: [
|
|
|
|
"SliceIter<Container>",
|
|
|
|
],
|
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
484..485,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:33:00 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn infer_call_method_return_associated_types_with_generic() {
|
|
|
|
check_types(
|
2020-03-24 13:33:00 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
pub trait Default {
|
|
|
|
fn default() -> Self;
|
|
|
|
}
|
|
|
|
pub trait Foo {
|
|
|
|
type Bar: Default;
|
|
|
|
}
|
2020-03-31 13:25:03 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
pub fn quux<T: Foo>() -> T::Bar {
|
|
|
|
let y = Default::default();
|
|
|
|
//^ <T as Foo>::Bar
|
|
|
|
|
|
|
|
y
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fn_hints() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
2021-06-17 03:18:37 -05:00
|
|
|
//- minicore: fn, sized
|
2021-06-04 06:47:39 -05:00
|
|
|
fn foo() -> impl Fn() { loop {} }
|
|
|
|
fn foo1() -> impl Fn(f64) { loop {} }
|
|
|
|
fn foo2() -> impl Fn(f64, f64) { loop {} }
|
|
|
|
fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} }
|
|
|
|
fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} }
|
|
|
|
fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} }
|
|
|
|
fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} }
|
|
|
|
fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = foo();
|
|
|
|
// ^^^ impl Fn()
|
|
|
|
let foo = foo1();
|
|
|
|
// ^^^ impl Fn(f64)
|
|
|
|
let foo = foo2();
|
|
|
|
// ^^^ impl Fn(f64, f64)
|
|
|
|
let foo = foo3();
|
|
|
|
// ^^^ impl Fn(f64, f64) -> u32
|
|
|
|
let foo = foo4();
|
|
|
|
// ^^^ &dyn Fn(f64, f64) -> u32
|
|
|
|
let foo = foo5();
|
|
|
|
// ^^^ &dyn Fn(&dyn Fn(f64, f64) -> u32, f64) -> u32
|
|
|
|
let foo = foo6();
|
2021-08-03 02:58:55 -05:00
|
|
|
// ^^^ impl Fn(f64, f64) -> u32
|
2021-06-04 06:47:39 -05:00
|
|
|
let foo = foo7();
|
2021-08-03 02:58:55 -05:00
|
|
|
// ^^^ *const impl Fn(f64, f64) -> u32
|
2020-06-30 11:04:25 -05:00
|
|
|
}
|
2021-06-04 06:47:39 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:48:01 -06:00
|
|
|
#[test]
|
|
|
|
fn check_hint_range_limit() {
|
|
|
|
let fixture = r#"
|
|
|
|
//- minicore: fn, sized
|
|
|
|
fn foo() -> impl Fn() { loop {} }
|
|
|
|
fn foo1() -> impl Fn(f64) { loop {} }
|
|
|
|
fn foo2() -> impl Fn(f64, f64) { loop {} }
|
|
|
|
fn foo3() -> impl Fn(f64, f64) -> u32 { loop {} }
|
|
|
|
fn foo4() -> &'static dyn Fn(f64, f64) -> u32 { loop {} }
|
|
|
|
fn foo5() -> &'static dyn Fn(&'static dyn Fn(f64, f64) -> u32, f64) -> u32 { loop {} }
|
|
|
|
fn foo6() -> impl Fn(f64, f64) -> u32 + Sized { loop {} }
|
|
|
|
fn foo7() -> *const (impl Fn(f64, f64) -> u32 + Sized) { loop {} }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = foo();
|
|
|
|
let foo = foo1();
|
|
|
|
let foo = foo2();
|
2022-05-13 12:42:59 -05:00
|
|
|
// ^^^ impl Fn(f64, f64)
|
2022-02-11 16:48:01 -06:00
|
|
|
let foo = foo3();
|
|
|
|
// ^^^ impl Fn(f64, f64) -> u32
|
|
|
|
let foo = foo4();
|
|
|
|
let foo = foo5();
|
|
|
|
let foo = foo6();
|
|
|
|
let foo = foo7();
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let (analysis, file_id) = fixture::file(fixture);
|
|
|
|
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
|
|
|
|
let inlay_hints = analysis
|
|
|
|
.inlay_hints(
|
2022-03-16 15:16:55 -05:00
|
|
|
&InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG },
|
2022-02-11 16:48:01 -06:00
|
|
|
file_id,
|
2022-11-07 10:21:37 -06:00
|
|
|
Some(TextRange::new(TextSize::from(500), TextSize::from(600))),
|
2022-02-11 16:48:01 -06:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual =
|
|
|
|
inlay_hints.into_iter().map(|it| (it.range, it.label.to_string())).collect::<Vec<_>>();
|
|
|
|
assert_eq!(expected, actual, "\nExpected:\n{:#?}\n\nActual:\n{:#?}", expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:01:00 -05:00
|
|
|
#[test]
|
|
|
|
fn fn_hints_ptr_rpit_fn_parentheses() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
|
|
|
//- minicore: fn, sized
|
|
|
|
trait Trait {}
|
|
|
|
|
|
|
|
fn foo1() -> *const impl Fn() { loop {} }
|
|
|
|
fn foo2() -> *const (impl Fn() + Sized) { loop {} }
|
|
|
|
fn foo3() -> *const (impl Fn() + ?Sized) { loop {} }
|
|
|
|
fn foo4() -> *const (impl Sized + Fn()) { loop {} }
|
|
|
|
fn foo5() -> *const (impl ?Sized + Fn()) { loop {} }
|
|
|
|
fn foo6() -> *const (impl Fn() + Trait) { loop {} }
|
|
|
|
fn foo7() -> *const (impl Fn() + Sized + Trait) { loop {} }
|
|
|
|
fn foo8() -> *const (impl Fn() + ?Sized + Trait) { loop {} }
|
|
|
|
fn foo9() -> *const (impl Fn() -> u8 + ?Sized) { loop {} }
|
|
|
|
fn foo10() -> *const (impl Fn() + Sized + ?Sized) { loop {} }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = foo1();
|
|
|
|
// ^^^ *const impl Fn()
|
|
|
|
let foo = foo2();
|
|
|
|
// ^^^ *const impl Fn()
|
|
|
|
let foo = foo3();
|
|
|
|
// ^^^ *const (impl Fn() + ?Sized)
|
|
|
|
let foo = foo4();
|
|
|
|
// ^^^ *const impl Fn()
|
|
|
|
let foo = foo5();
|
|
|
|
// ^^^ *const (impl Fn() + ?Sized)
|
|
|
|
let foo = foo6();
|
|
|
|
// ^^^ *const (impl Fn() + Trait)
|
|
|
|
let foo = foo7();
|
|
|
|
// ^^^ *const (impl Fn() + Trait)
|
|
|
|
let foo = foo8();
|
|
|
|
// ^^^ *const (impl Fn() + Trait + ?Sized)
|
|
|
|
let foo = foo9();
|
|
|
|
// ^^^ *const (impl Fn() -> u8 + ?Sized)
|
|
|
|
let foo = foo10();
|
|
|
|
// ^^^ *const impl Fn()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
#[test]
|
|
|
|
fn unit_structs_have_no_type_hints() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
2021-06-18 15:33:01 -05:00
|
|
|
//- minicore: result
|
2021-06-04 06:47:39 -05:00
|
|
|
struct SyntheticSyntax;
|
2020-03-24 13:33:00 -05:00
|
|
|
|
2020-06-30 11:04:25 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
match Ok(()) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(SyntheticSyntax) => (),
|
|
|
|
}
|
2020-06-30 11:04:25 -05:00
|
|
|
}"#,
|
2020-06-30 14:55:21 -05:00
|
|
|
);
|
2020-03-24 13:33:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn let_statement() {
|
|
|
|
check_types(
|
2020-03-24 13:33:00 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum Option<T> { None, Some(T) }
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
struct Test { a: Option<u32>, b: u8 }
|
2020-06-30 14:55:21 -05:00
|
|
|
|
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
struct InnerStruct {}
|
|
|
|
|
|
|
|
let test = 54;
|
|
|
|
//^^^^ i32
|
|
|
|
let test: i32 = 33;
|
|
|
|
let mut test = 33;
|
2021-07-31 08:24:21 -05:00
|
|
|
//^^^^ i32
|
2021-06-04 06:47:39 -05:00
|
|
|
let _ = 22;
|
|
|
|
let test = "test";
|
|
|
|
//^^^^ &str
|
|
|
|
let test = InnerStruct {};
|
|
|
|
//^^^^ InnerStruct
|
|
|
|
|
|
|
|
let test = unresolved();
|
|
|
|
|
|
|
|
let test = (42, 'a');
|
|
|
|
//^^^^ (i32, char)
|
|
|
|
let (a, (b, (c,)) = (2, (3, (9.2,));
|
|
|
|
//^ i32 ^ i32 ^ f64
|
|
|
|
let &x = &92;
|
|
|
|
//^ i32
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_expr() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
2021-06-18 15:38:19 -05:00
|
|
|
//- minicore: option
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Test { a: Option<u32>, b: u8 }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test = Some(Test { a: Some(3), b: 1 });
|
|
|
|
//^^^^ Option<Test>
|
|
|
|
if let None = &test {};
|
|
|
|
if let test = &test {};
|
|
|
|
//^^^^ &Option<Test>
|
|
|
|
if let Some(test) = &test {};
|
|
|
|
//^^^^ &Test
|
|
|
|
if let Some(Test { a, b }) = &test {};
|
|
|
|
//^ &Option<u32> ^ &u8
|
|
|
|
if let Some(Test { a: x, b: y }) = &test {};
|
|
|
|
//^ &Option<u32> ^ &u8
|
|
|
|
if let Some(Test { a: Some(x), b: y }) = &test {};
|
|
|
|
//^ &u32 ^ &u8
|
|
|
|
if let Some(Test { a: None, b: y }) = &test {};
|
|
|
|
//^ &u8
|
|
|
|
if let Some(Test { b: y, .. }) = &test {};
|
|
|
|
//^ &u8
|
|
|
|
if test == None {}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn while_expr() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
2021-06-18 15:38:19 -05:00
|
|
|
//- minicore: option
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Test { a: Option<u32>, b: u8 }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test = Some(Test { a: Some(3), b: 1 });
|
|
|
|
//^^^^ Option<Test>
|
|
|
|
while let Some(Test { a: Some(x), b: y }) = &test {};
|
|
|
|
//^ &u32 ^ &u8
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn match_arm_list() {
|
|
|
|
check_types(
|
|
|
|
r#"
|
2021-06-18 15:38:19 -05:00
|
|
|
//- minicore: option
|
2021-06-04 06:47:39 -05:00
|
|
|
struct Test { a: Option<u32>, b: u8 }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match Some(Test { a: Some(3), b: 1 }) {
|
|
|
|
None => (),
|
|
|
|
test => (),
|
|
|
|
//^^^^ Option<Test>
|
|
|
|
Some(Test { a: Some(x), b: y }) => (),
|
|
|
|
//^ u32 ^ u8
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}"#,
|
2020-06-30 14:55:21 -05:00
|
|
|
);
|
2020-03-24 13:33:00 -05:00
|
|
|
}
|
2020-09-13 12:24:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complete_for_hint() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_types(
|
2020-09-13 12:24:04 -05:00
|
|
|
r#"
|
2021-06-17 03:18:37 -05:00
|
|
|
//- minicore: iterator
|
2020-10-03 00:37:58 -05:00
|
|
|
pub struct Vec<T> {}
|
|
|
|
|
|
|
|
impl<T> Vec<T> {
|
|
|
|
pub fn new() -> Self { Vec {} }
|
|
|
|
pub fn push(&mut self, t: T) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoIterator for Vec<T> {
|
2022-09-29 04:44:45 -05:00
|
|
|
type Item = T;
|
|
|
|
type IntoIter = IntoIter<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct IntoIter<T> {}
|
|
|
|
|
|
|
|
impl<T> Iterator for IntoIter<T> {
|
|
|
|
type Item = T;
|
2020-10-03 00:37:58 -05:00
|
|
|
}
|
|
|
|
|
2020-09-13 12:24:04 -05:00
|
|
|
fn main() {
|
2020-10-03 00:37:58 -05:00
|
|
|
let mut data = Vec::new();
|
2021-07-31 08:24:21 -05:00
|
|
|
//^^^^ Vec<&str>
|
2020-10-03 00:37:58 -05:00
|
|
|
data.push("foo");
|
|
|
|
for i in data {
|
|
|
|
//^ &str
|
|
|
|
let z = i;
|
|
|
|
//^ &str
|
2020-09-13 12:24:04 -05:00
|
|
|
}
|
2020-10-03 00:37:58 -05:00
|
|
|
}
|
2020-10-06 07:40:27 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multi_dyn_trait_bounds() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_types(
|
2020-10-06 07:40:27 -05:00
|
|
|
r#"
|
|
|
|
pub struct Vec<T> {}
|
|
|
|
|
|
|
|
impl<T> Vec<T> {
|
|
|
|
pub fn new() -> Self { Vec {} }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Box<T> {}
|
|
|
|
|
|
|
|
trait Display {}
|
2022-07-18 05:49:14 -05:00
|
|
|
auto trait Sync {}
|
2020-10-06 07:40:27 -05:00
|
|
|
|
|
|
|
fn main() {
|
2021-10-03 08:31:35 -05:00
|
|
|
// The block expression wrapping disables the constructor hint hiding logic
|
|
|
|
let _v = { Vec::<Box<&(dyn Display + Sync)>>::new() };
|
2020-10-06 07:40:27 -05:00
|
|
|
//^^ Vec<Box<&(dyn Display + Sync)>>
|
2021-10-03 08:31:35 -05:00
|
|
|
let _v = { Vec::<Box<*const (dyn Display + Sync)>>::new() };
|
2020-10-06 07:40:27 -05:00
|
|
|
//^^ Vec<Box<*const (dyn Display + Sync)>>
|
2021-10-03 08:31:35 -05:00
|
|
|
let _v = { Vec::<Box<dyn Display + Sync>>::new() };
|
2020-10-06 07:40:27 -05:00
|
|
|
//^^ Vec<Box<dyn Display + Sync>>
|
|
|
|
}
|
2020-10-06 12:07:34 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shorten_iterator_hints() {
|
2021-06-04 06:47:39 -05:00
|
|
|
check_types(
|
2020-10-07 03:14:42 -05:00
|
|
|
r#"
|
2021-06-17 03:28:44 -05:00
|
|
|
//- minicore: iterators
|
2020-10-07 04:30:42 -05:00
|
|
|
use core::iter;
|
2020-10-06 12:07:34 -05:00
|
|
|
|
2020-10-06 14:05:57 -05:00
|
|
|
struct MyIter;
|
|
|
|
|
2020-10-07 03:14:42 -05:00
|
|
|
impl Iterator for MyIter {
|
2020-10-06 14:05:57 -05:00
|
|
|
type Item = ();
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 12:07:34 -05:00
|
|
|
fn main() {
|
2020-10-06 14:05:57 -05:00
|
|
|
let _x = MyIter;
|
|
|
|
//^^ MyIter
|
2020-10-06 12:07:34 -05:00
|
|
|
let _x = iter::repeat(0);
|
|
|
|
//^^ impl Iterator<Item = i32>
|
|
|
|
fn generic<T: Clone>(t: T) {
|
|
|
|
let _x = iter::repeat(t);
|
|
|
|
//^^ impl Iterator<Item = T>
|
2020-10-07 04:30:42 -05:00
|
|
|
let _chained = iter::repeat(t).take(10);
|
|
|
|
//^^^^^^^^ impl Iterator<Item = T>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:45:21 -05:00
|
|
|
#[test]
|
2021-12-21 10:26:37 -06:00
|
|
|
fn skip_constructor_and_enum_type_hints() {
|
2021-11-13 17:39:34 -06:00
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig {
|
|
|
|
type_hints: true,
|
|
|
|
hide_named_constructor_hints: true,
|
2022-03-16 15:16:55 -05:00
|
|
|
..DISABLED_CONFIG
|
2021-11-13 17:39:34 -06:00
|
|
|
},
|
2021-10-03 07:45:21 -05:00
|
|
|
r#"
|
2021-12-21 10:26:37 -06:00
|
|
|
//- minicore: try, option
|
2021-10-03 07:45:21 -05:00
|
|
|
use core::ops::ControlFlow;
|
|
|
|
|
2021-12-22 09:29:26 -06:00
|
|
|
mod x {
|
|
|
|
pub mod y { pub struct Foo; }
|
|
|
|
pub struct Foo;
|
|
|
|
pub enum AnotherEnum {
|
|
|
|
Variant()
|
|
|
|
};
|
|
|
|
}
|
2021-10-03 07:45:21 -05:00
|
|
|
struct Struct;
|
|
|
|
struct TupleStruct();
|
|
|
|
|
|
|
|
impl Struct {
|
|
|
|
fn new() -> Self {
|
|
|
|
Struct
|
|
|
|
}
|
|
|
|
fn try_new() -> ControlFlow<(), Self> {
|
|
|
|
ControlFlow::Continue(Struct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Generic<T>(T);
|
|
|
|
impl Generic<i32> {
|
|
|
|
fn new() -> Self {
|
|
|
|
Generic(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 10:26:37 -06:00
|
|
|
enum Enum {
|
|
|
|
Variant(u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn times2(value: i32) -> i32 {
|
|
|
|
2 * value
|
|
|
|
}
|
|
|
|
|
2021-10-03 07:45:21 -05:00
|
|
|
fn main() {
|
2021-12-21 10:26:37 -06:00
|
|
|
let enumb = Enum::Variant(0);
|
|
|
|
|
2021-12-22 09:29:26 -06:00
|
|
|
let strukt = x::Foo;
|
|
|
|
let strukt = x::y::Foo;
|
2021-12-21 10:26:37 -06:00
|
|
|
let strukt = Struct;
|
2021-10-03 07:45:21 -05:00
|
|
|
let strukt = Struct::new();
|
2021-12-21 10:26:37 -06:00
|
|
|
|
2021-10-03 07:45:21 -05:00
|
|
|
let tuple_struct = TupleStruct();
|
2021-12-21 10:26:37 -06:00
|
|
|
|
2021-10-03 07:45:21 -05:00
|
|
|
let generic0 = Generic::new();
|
2021-12-21 10:26:37 -06:00
|
|
|
// ^^^^^^^^ Generic<i32>
|
|
|
|
let generic1 = Generic(0);
|
|
|
|
// ^^^^^^^^ Generic<i32>
|
|
|
|
let generic2 = Generic::<i32>::new();
|
|
|
|
let generic3 = <Generic<i32>>::new();
|
|
|
|
let generic4 = Generic::<i32>(0);
|
|
|
|
|
|
|
|
|
|
|
|
let option = Some(0);
|
|
|
|
// ^^^^^^ Option<i32>
|
|
|
|
let func = times2;
|
|
|
|
// ^^^^ fn times2(i32) -> i32
|
|
|
|
let closure = |x: i32| x * 2;
|
|
|
|
// ^^^^^^^ |i32| -> i32
|
2021-10-03 07:45:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fallible() -> ControlFlow<()> {
|
|
|
|
let strukt = Struct::try_new()?;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-13 17:12:29 -06:00
|
|
|
#[test]
|
|
|
|
fn shows_constructor_type_hints_when_enabled() {
|
2021-11-13 17:39:34 -06:00
|
|
|
check_types(
|
2021-11-13 17:12:29 -06:00
|
|
|
r#"
|
|
|
|
//- minicore: try
|
|
|
|
use core::ops::ControlFlow;
|
|
|
|
|
|
|
|
struct Struct;
|
|
|
|
struct TupleStruct();
|
|
|
|
|
|
|
|
impl Struct {
|
|
|
|
fn new() -> Self {
|
|
|
|
Struct
|
|
|
|
}
|
|
|
|
fn try_new() -> ControlFlow<(), Self> {
|
|
|
|
ControlFlow::Continue(Struct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Generic<T>(T);
|
|
|
|
impl Generic<i32> {
|
|
|
|
fn new() -> Self {
|
|
|
|
Generic(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let strukt = Struct::new();
|
|
|
|
// ^^^^^^ Struct
|
|
|
|
let tuple_struct = TupleStruct();
|
|
|
|
// ^^^^^^^^^^^^ TupleStruct
|
|
|
|
let generic0 = Generic::new();
|
|
|
|
// ^^^^^^^^ Generic<i32>
|
|
|
|
let generic1 = Generic::<i32>::new();
|
|
|
|
// ^^^^^^^^ Generic<i32>
|
|
|
|
let generic2 = <Generic<i32>>::new();
|
|
|
|
// ^^^^^^^^ Generic<i32>
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fallible() -> ControlFlow<()> {
|
|
|
|
let strukt = Struct::try_new()?;
|
|
|
|
// ^^^^^^ Struct
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-07 04:30:42 -05:00
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn closures() {
|
|
|
|
check(
|
2020-10-07 04:30:42 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
fn main() {
|
|
|
|
let mut start = 0;
|
2021-07-31 08:24:21 -05:00
|
|
|
//^^^^^ i32
|
2022-03-16 15:16:55 -05:00
|
|
|
(0..2).for_each(|increment | { start += increment; });
|
2021-06-04 06:47:39 -05:00
|
|
|
//^^^^^^^^^ i32
|
2020-10-07 04:30:42 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
let multiply =
|
2021-11-22 11:27:03 -06:00
|
|
|
//^^^^^^^^ |i32, i32| -> i32
|
2021-06-04 06:47:39 -05:00
|
|
|
| a, b| a * b
|
|
|
|
//^ i32 ^ i32
|
2022-03-16 15:16:55 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
;
|
2020-10-07 04:30:42 -05:00
|
|
|
|
2022-05-19 11:53:08 -05:00
|
|
|
let _: i32 = multiply(1, 2);
|
|
|
|
//^ a ^ b
|
2021-06-04 06:47:39 -05:00
|
|
|
let multiply_ref = &multiply;
|
2021-11-22 11:27:03 -06:00
|
|
|
//^^^^^^^^^^^^ &|i32, i32| -> i32
|
2021-06-04 06:47:39 -05:00
|
|
|
|
|
|
|
let return_42 = || 42;
|
|
|
|
//^^^^^^^^^ || -> i32
|
2022-03-16 15:16:55 -05:00
|
|
|
|| { 42 };
|
|
|
|
//^^ i32
|
2021-06-04 06:47:39 -05:00
|
|
|
}"#,
|
|
|
|
);
|
2020-10-06 12:07:34 -05:00
|
|
|
}
|
2021-06-04 06:47:39 -05:00
|
|
|
|
2022-05-28 07:13:25 -05:00
|
|
|
#[test]
|
|
|
|
fn return_type_hints_for_closure_without_block() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig {
|
|
|
|
closure_return_type_hints: ClosureReturnTypeHints::Always,
|
|
|
|
..DISABLED_CONFIG
|
|
|
|
},
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let a = || { 0 };
|
|
|
|
//^^ i32
|
|
|
|
let b = || 0;
|
|
|
|
//^^ i32
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-15 06:17:52 -05:00
|
|
|
#[test]
|
|
|
|
fn skip_closure_type_hints() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig {
|
|
|
|
type_hints: true,
|
|
|
|
hide_closure_initialization_hints: true,
|
|
|
|
..DISABLED_CONFIG
|
|
|
|
},
|
|
|
|
r#"
|
|
|
|
//- minicore: fn
|
|
|
|
fn main() {
|
|
|
|
let multiple_2 = |x: i32| { x * 2 };
|
|
|
|
|
|
|
|
let multiple_2 = |x: i32| x * 2;
|
|
|
|
// ^^^^^^^^^^ |i32| -> i32
|
|
|
|
|
|
|
|
let (not) = (|x: bool| { !x });
|
|
|
|
// ^^^ |bool| -> bool
|
|
|
|
|
|
|
|
let (is_zero, _b) = (|x: usize| { x == 0 }, false);
|
|
|
|
// ^^^^^^^ |usize| -> bool
|
|
|
|
// ^^ bool
|
|
|
|
|
|
|
|
let plus_one = |x| { x + 1 };
|
|
|
|
// ^ u8
|
|
|
|
foo(plus_one);
|
|
|
|
|
|
|
|
let add_mul = bar(|x: u8| { x + 1 });
|
|
|
|
// ^^^^^^^ impl FnOnce(u8) -> u8 + ?Sized
|
|
|
|
|
|
|
|
let closure = if let Some(6) = add_mul(2).checked_sub(1) {
|
|
|
|
// ^^^^^^^ fn(i32) -> i32
|
|
|
|
|x: i32| { x * 2 }
|
|
|
|
} else {
|
|
|
|
|x: i32| { x * 3 }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(f: impl FnOnce(u8) -> u8) {}
|
|
|
|
|
|
|
|
fn bar(f: impl FnOnce(u8) -> u8) -> impl FnOnce(u8) -> u8 {
|
|
|
|
move |x: u8| f(x) * 2
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
#[test]
|
|
|
|
fn hint_truncation() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig { max_length: Some(8), ..TEST_CONFIG },
|
|
|
|
r#"
|
|
|
|
struct Smol<T>(T);
|
|
|
|
|
|
|
|
struct VeryLongOuterName<T>(T);
|
2020-10-06 12:07:34 -05:00
|
|
|
|
2020-10-07 04:30:42 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let a = Smol(0u32);
|
|
|
|
//^ Smol<u32>
|
|
|
|
let b = VeryLongOuterName(0usize);
|
|
|
|
//^ VeryLongOuterName<…>
|
|
|
|
let c = Smol(Smol(0u32))
|
|
|
|
//^ Smol<Smol<…>>
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chaining hint tests
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chaining_hints_ignore_comments() {
|
|
|
|
check_expect(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { type_hints: false, chaining_hints: true, ..DISABLED_CONFIG },
|
2021-06-04 06:47:39 -05:00
|
|
|
r#"
|
|
|
|
struct A(B);
|
|
|
|
impl A { fn into_b(self) -> B { self.0 } }
|
|
|
|
struct B(C);
|
|
|
|
impl B { fn into_c(self) -> C { self.0 } }
|
|
|
|
struct C;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let c = A(B(C))
|
|
|
|
.into_b() // This is a comment
|
|
|
|
// This is another comment
|
|
|
|
.into_c();
|
2020-10-07 04:30:42 -05:00
|
|
|
}
|
2020-10-03 00:37:58 -05:00
|
|
|
"#,
|
2020-10-07 04:30:42 -05:00
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 147..172,
|
2020-10-07 04:30:42 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"B",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
147..172,
|
|
|
|
),
|
|
|
|
),
|
2020-10-07 04:30:42 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 147..154,
|
2020-10-07 04:30:42 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"A",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
147..154,
|
|
|
|
),
|
|
|
|
),
|
2020-10-07 04:30:42 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
2020-09-13 12:24:04 -05:00
|
|
|
);
|
|
|
|
}
|
2020-10-10 11:51:02 -05:00
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn chaining_hints_without_newlines() {
|
|
|
|
check_chains(
|
|
|
|
r#"
|
|
|
|
struct A(B);
|
|
|
|
impl A { fn into_b(self) -> B { self.0 } }
|
|
|
|
struct B(C);
|
|
|
|
impl B { fn into_c(self) -> C { self.0 } }
|
|
|
|
struct C;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let c = A(B(C)).into_b().into_c();
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn struct_access_chaining_hints() {
|
|
|
|
check_expect(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
|
2020-10-10 11:51:02 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
struct A { pub b: B }
|
|
|
|
struct B { pub c: C }
|
|
|
|
struct C(pub bool);
|
|
|
|
struct D;
|
2020-10-10 11:51:02 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
impl D {
|
|
|
|
fn foo(&self) -> i32 { 42 }
|
2020-10-10 11:51:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let x = A { b: B { c: C(true) } }
|
|
|
|
.b
|
|
|
|
.c
|
|
|
|
.0;
|
|
|
|
let x = D
|
|
|
|
.foo();
|
|
|
|
}"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 143..190,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"C",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
143..190,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 143..179,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"B",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
143..179,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
2020-10-22 12:42:39 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn generic_chaining_hints() {
|
|
|
|
check_expect(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
|
2020-10-22 12:42:39 -05:00
|
|
|
r#"
|
2021-06-04 06:47:39 -05:00
|
|
|
struct A<T>(T);
|
|
|
|
struct B<T>(T);
|
|
|
|
struct C<T>(T);
|
|
|
|
struct X<T,R>(T, R);
|
2020-10-22 12:42:39 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
impl<T> A<T> {
|
|
|
|
fn new(t: T) -> Self { A(t) }
|
|
|
|
fn into_b(self) -> B<T> { B(self.0) }
|
|
|
|
}
|
|
|
|
impl<T> B<T> {
|
|
|
|
fn into_c(self) -> C<T> { C(self.0) }
|
|
|
|
}
|
2020-10-22 12:42:39 -05:00
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let c = A::new(X(42, true))
|
|
|
|
.into_b()
|
|
|
|
.into_c();
|
2020-10-22 12:42:39 -05:00
|
|
|
}
|
2020-10-10 11:51:02 -05:00
|
|
|
"#,
|
2021-06-04 06:47:39 -05:00
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 246..283,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"B<X<i32, bool>>",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
246..283,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 246..265,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"A<X<i32, bool>>",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
246..265,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
2020-10-10 11:51:02 -05:00
|
|
|
);
|
|
|
|
}
|
2020-10-28 06:29:42 -05:00
|
|
|
|
|
|
|
#[test]
|
2021-06-04 06:47:39 -05:00
|
|
|
fn shorten_iterator_chaining_hints() {
|
|
|
|
check_expect(
|
2022-03-16 15:16:55 -05:00
|
|
|
InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG },
|
2020-10-28 06:29:42 -05:00
|
|
|
r#"
|
2021-06-17 03:28:44 -05:00
|
|
|
//- minicore: iterators
|
2021-06-04 06:47:39 -05:00
|
|
|
use core::iter;
|
2020-10-28 06:29:42 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
struct MyIter;
|
2020-10-28 06:29:42 -05:00
|
|
|
|
2021-06-04 06:47:39 -05:00
|
|
|
impl Iterator for MyIter {
|
|
|
|
type Item = ();
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
None
|
2020-10-28 06:29:42 -05:00
|
|
|
}
|
2020-10-29 07:02:34 -05:00
|
|
|
}
|
2021-01-12 13:19:13 -06:00
|
|
|
|
|
|
|
fn main() {
|
2021-06-04 06:47:39 -05:00
|
|
|
let _x = MyIter.by_ref()
|
|
|
|
.take(5)
|
|
|
|
.by_ref()
|
|
|
|
.take(5)
|
|
|
|
.by_ref();
|
2021-01-12 13:19:13 -06:00
|
|
|
}
|
2020-10-29 07:02:34 -05:00
|
|
|
"#,
|
2021-06-04 06:47:39 -05:00
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 174..241,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"impl Iterator<Item = ()>",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
174..241,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 174..224,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"impl Iterator<Item = ()>",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
174..224,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 174..206,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"impl Iterator<Item = ()>",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
174..206,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
2021-06-17 03:18:37 -05:00
|
|
|
range: 174..189,
|
2021-06-04 06:47:39 -05:00
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"&mut MyIter",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
174..189,
|
|
|
|
),
|
|
|
|
),
|
2021-06-04 06:47:39 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
2021-01-27 07:20:58 -06:00
|
|
|
}
|
2021-09-18 06:19:29 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hints_in_attr_call() {
|
|
|
|
check_expect(
|
|
|
|
TEST_CONFIG,
|
|
|
|
r#"
|
|
|
|
//- proc_macros: identity, input_replace
|
|
|
|
struct Struct;
|
|
|
|
impl Struct {
|
|
|
|
fn chain(self) -> Self {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[proc_macros::identity]
|
|
|
|
fn main() {
|
|
|
|
let strukt = Struct;
|
|
|
|
strukt
|
|
|
|
.chain()
|
|
|
|
.chain()
|
|
|
|
.chain();
|
|
|
|
Struct::chain(strukt);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
InlayHint {
|
|
|
|
range: 124..130,
|
|
|
|
kind: TypeHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"Struct",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
124..130,
|
|
|
|
),
|
|
|
|
),
|
2021-09-18 06:19:29 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: 145..185,
|
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"Struct",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
145..185,
|
|
|
|
),
|
|
|
|
),
|
2021-09-18 06:19:29 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: 145..168,
|
|
|
|
kind: ChainingHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"Struct",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverRanged(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
2022-05-17 07:58:26 -05:00
|
|
|
145..168,
|
|
|
|
),
|
|
|
|
),
|
2021-09-18 06:19:29 -05:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: 222..228,
|
|
|
|
kind: ParameterHint,
|
2022-08-31 11:34:10 -05:00
|
|
|
label: [
|
|
|
|
"self",
|
|
|
|
],
|
2022-05-19 08:20:45 -05:00
|
|
|
tooltip: Some(
|
|
|
|
HoverOffset(
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
42,
|
|
|
|
),
|
|
|
|
),
|
2021-09-18 06:19:29 -05:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2022-03-18 12:11:16 -05:00
|
|
|
|
|
|
|
#[test]
|
2022-03-18 12:57:11 -05:00
|
|
|
fn hints_lifetimes() {
|
2022-03-18 12:11:16 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn empty() {}
|
|
|
|
|
|
|
|
fn no_gpl(a: &()) {}
|
2022-03-19 12:11:56 -05:00
|
|
|
//^^^^^^<'0>
|
|
|
|
// ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
fn empty_gpl<>(a: &()) {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^'0 ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
fn partial<'b>(a: &(), b: &'b ()) {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^'0, $ ^'0
|
2022-03-18 12:55:03 -05:00
|
|
|
fn partial<'a>(a: &'a (), b: &()) {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^'0, $ ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
|
|
|
|
fn single_ret(a: &()) -> &() {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^^^^^^^^^^<'0>
|
|
|
|
// ^'0 ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
fn full_mul(a: &(), b: &()) {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^^^^^^^^<'0, '1>
|
|
|
|
// ^'0 ^'1
|
2022-03-18 12:11:16 -05:00
|
|
|
|
|
|
|
fn foo<'c>(a: &'c ()) -> &() {}
|
|
|
|
// ^'c
|
|
|
|
|
2022-03-19 12:11:56 -05:00
|
|
|
fn nested_in(a: & &X< &()>) {}
|
|
|
|
// ^^^^^^^^^<'0, '1, '2>
|
|
|
|
//^'0 ^'1 ^'2
|
|
|
|
fn nested_out(a: &()) -> & &X< &()>{}
|
|
|
|
// ^^^^^^^^^^<'0>
|
|
|
|
//^'0 ^'0 ^'0 ^'0
|
|
|
|
|
2022-03-18 12:11:16 -05:00
|
|
|
impl () {
|
2022-03-19 14:12:14 -05:00
|
|
|
fn foo(&self) {}
|
|
|
|
// ^^^<'0>
|
|
|
|
// ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
fn foo(&self) -> &() {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^^^<'0>
|
|
|
|
// ^'0 ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
fn foo(&self, a: &()) -> &() {}
|
2022-03-19 12:11:56 -05:00
|
|
|
// ^^^<'0, '1>
|
|
|
|
// ^'0 ^'1 ^'0
|
2022-03-18 12:11:16 -05:00
|
|
|
}
|
2022-03-19 13:01:19 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hints_lifetimes_named() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
|
|
|
|
r#"
|
|
|
|
fn nested_in<'named>(named: & &X< &()>) {}
|
|
|
|
// ^'named1, 'named2, 'named3, $
|
|
|
|
//^'named1 ^'named2 ^'named3
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-03-19 14:12:14 -05:00
|
|
|
fn hints_lifetimes_trivial_skip() {
|
2022-03-19 13:01:19 -05:00
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig {
|
|
|
|
lifetime_elision_hints: LifetimeElisionHints::SkipTrivial,
|
|
|
|
..TEST_CONFIG
|
|
|
|
},
|
|
|
|
r#"
|
2022-03-19 14:12:14 -05:00
|
|
|
fn no_gpl(a: &()) {}
|
|
|
|
fn empty_gpl<>(a: &()) {}
|
|
|
|
fn partial<'b>(a: &(), b: &'b ()) {}
|
2022-03-19 13:01:19 -05:00
|
|
|
fn partial<'a>(a: &'a (), b: &()) {}
|
2022-03-19 14:12:14 -05:00
|
|
|
|
|
|
|
fn single_ret(a: &()) -> &() {}
|
|
|
|
// ^^^^^^^^^^<'0>
|
|
|
|
// ^'0 ^'0
|
|
|
|
fn full_mul(a: &(), b: &()) {}
|
|
|
|
|
|
|
|
fn foo<'c>(a: &'c ()) -> &() {}
|
|
|
|
// ^'c
|
|
|
|
|
|
|
|
fn nested_in(a: & &X< &()>) {}
|
|
|
|
fn nested_out(a: &()) -> & &X< &()>{}
|
|
|
|
// ^^^^^^^^^^<'0>
|
|
|
|
//^'0 ^'0 ^'0 ^'0
|
2022-03-19 13:01:19 -05:00
|
|
|
|
|
|
|
impl () {
|
2022-03-19 14:12:14 -05:00
|
|
|
fn foo(&self) {}
|
2022-03-19 13:01:19 -05:00
|
|
|
fn foo(&self) -> &() {}
|
2022-03-19 14:12:14 -05:00
|
|
|
// ^^^<'0>
|
|
|
|
// ^'0 ^'0
|
2022-03-19 13:01:19 -05:00
|
|
|
fn foo(&self, a: &()) -> &() {}
|
|
|
|
// ^^^<'0, '1>
|
|
|
|
// ^'0 ^'1 ^'0
|
|
|
|
}
|
2022-03-20 08:38:16 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-30 06:51:48 -05:00
|
|
|
#[test]
|
|
|
|
fn hints_lifetimes_static() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig {
|
|
|
|
lifetime_elision_hints: LifetimeElisionHints::Always,
|
|
|
|
..TEST_CONFIG
|
|
|
|
},
|
|
|
|
r#"
|
|
|
|
trait Trait {}
|
|
|
|
static S: &str = "";
|
|
|
|
// ^'static
|
|
|
|
const C: &str = "";
|
|
|
|
// ^'static
|
|
|
|
const C: &dyn Trait = panic!();
|
|
|
|
// ^'static
|
|
|
|
|
|
|
|
impl () {
|
|
|
|
const C: &str = "";
|
|
|
|
const C: &dyn Trait = panic!();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-14 07:26:08 -05:00
|
|
|
#[test]
|
|
|
|
fn hints_binding_modes() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig { binding_mode_hints: true, ..DISABLED_CONFIG },
|
|
|
|
r#"
|
|
|
|
fn __(
|
|
|
|
(x,): (u32,),
|
|
|
|
(x,): &(u32,),
|
|
|
|
//^^^^&
|
|
|
|
//^ ref
|
|
|
|
(x,): &mut (u32,)
|
|
|
|
//^^^^&mut
|
|
|
|
//^ ref mut
|
|
|
|
) {
|
|
|
|
let (x,) = (0,);
|
|
|
|
let (x,) = &(0,);
|
|
|
|
//^^^^ &
|
|
|
|
//^ ref
|
|
|
|
let (x,) = &mut (0,);
|
|
|
|
//^^^^ &mut
|
|
|
|
//^ ref mut
|
|
|
|
let &mut (x,) = &mut (0,);
|
|
|
|
let (ref mut x,) = &mut (0,);
|
|
|
|
//^^^^^^^^^^^^ &mut
|
|
|
|
let &mut (ref mut x,) = &mut (0,);
|
|
|
|
let (mut x,) = &mut (0,);
|
|
|
|
//^^^^^^^^ &mut
|
|
|
|
match (0,) {
|
|
|
|
(x,) => ()
|
|
|
|
}
|
|
|
|
match &(0,) {
|
2022-12-16 13:52:31 -06:00
|
|
|
(x,) | (x,) => (),
|
|
|
|
//^^^^^^^^^^^&
|
2022-05-14 07:26:08 -05:00
|
|
|
//^ ref
|
2022-12-16 13:52:31 -06:00
|
|
|
//^ ref
|
|
|
|
//^^^^^^^^^^^(
|
|
|
|
//^^^^^^^^^^^)
|
|
|
|
((x,) | (x,)) => (),
|
2022-12-16 14:16:55 -06:00
|
|
|
//^^^^^^^^^^^^^&
|
2022-12-16 13:52:31 -06:00
|
|
|
//^ ref
|
|
|
|
//^ ref
|
2022-05-14 07:26:08 -05:00
|
|
|
}
|
|
|
|
match &mut (0,) {
|
|
|
|
(x,) => ()
|
|
|
|
//^^^^ &mut
|
|
|
|
//^ ref mut
|
|
|
|
}
|
2022-05-13 12:42:59 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hints_closing_brace() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG },
|
|
|
|
r#"
|
|
|
|
fn a() {}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
} // no hint unless `}` is the last token on the line
|
|
|
|
|
|
|
|
fn g() {
|
|
|
|
}
|
|
|
|
//^ fn g
|
|
|
|
|
|
|
|
fn h<T>(with: T, arguments: u8, ...) {
|
|
|
|
}
|
|
|
|
//^ fn h
|
|
|
|
|
|
|
|
trait Tr {
|
|
|
|
fn f();
|
|
|
|
fn g() {
|
|
|
|
}
|
|
|
|
//^ fn g
|
|
|
|
}
|
|
|
|
//^ trait Tr
|
|
|
|
impl Tr for () {
|
|
|
|
}
|
|
|
|
//^ impl Tr for ()
|
|
|
|
impl dyn Tr {
|
|
|
|
}
|
|
|
|
//^ impl dyn Tr
|
|
|
|
|
|
|
|
static S0: () = 0;
|
|
|
|
static S1: () = {};
|
|
|
|
static S2: () = {
|
|
|
|
};
|
|
|
|
//^ static S2
|
|
|
|
const _: () = {
|
|
|
|
};
|
|
|
|
//^ const _
|
|
|
|
|
|
|
|
mod m {
|
|
|
|
}
|
|
|
|
//^ mod m
|
2022-05-16 08:23:25 -05:00
|
|
|
|
|
|
|
m! {}
|
|
|
|
m!();
|
|
|
|
m!(
|
|
|
|
);
|
|
|
|
//^ m!
|
|
|
|
|
|
|
|
m! {
|
|
|
|
}
|
|
|
|
//^ m!
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let v = vec![
|
|
|
|
];
|
|
|
|
}
|
|
|
|
//^ fn f
|
2022-03-18 12:11:16 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2022-11-04 16:40:06 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn adjustment_hints() {
|
|
|
|
check_with_config(
|
|
|
|
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
|
|
|
|
r#"
|
|
|
|
//- minicore: coerce_unsized
|
|
|
|
fn main() {
|
|
|
|
let _: u32 = loop {};
|
|
|
|
//^^^^^^^<never-to-any>
|
|
|
|
let _: &u32 = &mut 0;
|
|
|
|
//^^^^^^&
|
|
|
|
//^^^^^^*
|
|
|
|
let _: &mut u32 = &mut 0;
|
|
|
|
//^^^^^^&mut $
|
|
|
|
//^^^^^^*
|
|
|
|
let _: *const u32 = &mut 0;
|
|
|
|
//^^^^^^&raw const $
|
|
|
|
//^^^^^^*
|
|
|
|
let _: *mut u32 = &mut 0;
|
|
|
|
//^^^^^^&raw mut $
|
|
|
|
//^^^^^^*
|
|
|
|
let _: fn() = main;
|
|
|
|
//^^^^<fn-item-to-fn-pointer>
|
|
|
|
let _: unsafe fn() = main;
|
|
|
|
//^^^^<safe-fn-pointer-to-unsafe-fn-pointer>
|
|
|
|
//^^^^<fn-item-to-fn-pointer>
|
|
|
|
let _: unsafe fn() = main as fn();
|
|
|
|
//^^^^^^^^^^^^<safe-fn-pointer-to-unsafe-fn-pointer>
|
|
|
|
let _: fn() = || {};
|
|
|
|
//^^^^^<closure-to-fn-pointer>
|
|
|
|
let _: unsafe fn() = || {};
|
|
|
|
//^^^^^<closure-to-unsafe-fn-pointer>
|
|
|
|
let _: *const u32 = &mut 0u32 as *mut u32;
|
|
|
|
//^^^^^^^^^^^^^^^^^^^^^<mut-ptr-to-const-ptr>
|
|
|
|
let _: &mut [_] = &mut [0; 0];
|
|
|
|
//^^^^^^^^^^^<unsize>
|
|
|
|
//^^^^^^^^^^^&mut $
|
|
|
|
//^^^^^^^^^^^*
|
|
|
|
|
|
|
|
Struct.consume();
|
|
|
|
Struct.by_ref();
|
|
|
|
//^^^^^^(
|
|
|
|
//^^^^^^&
|
|
|
|
//^^^^^^)
|
|
|
|
Struct.by_ref_mut();
|
|
|
|
//^^^^^^(
|
|
|
|
//^^^^^^&mut $
|
|
|
|
//^^^^^^)
|
|
|
|
|
|
|
|
(&Struct).consume();
|
|
|
|
//^^^^^^^*
|
|
|
|
(&Struct).by_ref();
|
|
|
|
|
|
|
|
(&mut Struct).consume();
|
|
|
|
//^^^^^^^^^^^*
|
|
|
|
(&mut Struct).by_ref();
|
|
|
|
//^^^^^^^^^^^&
|
|
|
|
//^^^^^^^^^^^*
|
|
|
|
(&mut Struct).by_ref_mut();
|
2022-12-09 11:40:54 -06:00
|
|
|
|
|
|
|
// Check that block-like expressions don't duplicate hints
|
|
|
|
let _: &mut [u32] = (&mut []);
|
|
|
|
//^^^^^^^<unsize>
|
|
|
|
//^^^^^^^&mut $
|
|
|
|
//^^^^^^^*
|
|
|
|
let _: &mut [u32] = { &mut [] };
|
|
|
|
//^^^^^^^<unsize>
|
|
|
|
//^^^^^^^&mut $
|
|
|
|
//^^^^^^^*
|
|
|
|
let _: &mut [u32] = unsafe { &mut [] };
|
|
|
|
//^^^^^^^<unsize>
|
|
|
|
//^^^^^^^&mut $
|
|
|
|
//^^^^^^^*
|
|
|
|
let _: &mut [u32] = if true {
|
|
|
|
&mut []
|
|
|
|
//^^^^^^^<unsize>
|
|
|
|
//^^^^^^^&mut $
|
|
|
|
//^^^^^^^*
|
|
|
|
} else {
|
|
|
|
loop {}
|
|
|
|
//^^^^^^^<never-to-any>
|
|
|
|
};
|
|
|
|
let _: &mut [u32] = match () { () => &mut [] }
|
|
|
|
//^^^^^^^<unsize>
|
|
|
|
//^^^^^^^&mut $
|
|
|
|
//^^^^^^^*
|
2022-11-04 16:40:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Struct;
|
|
|
|
impl Struct {
|
|
|
|
fn consume(self) {}
|
|
|
|
fn by_ref(&self) {}
|
|
|
|
fn by_ref_mut(&mut self) {}
|
|
|
|
}
|
2022-12-09 11:40:54 -06:00
|
|
|
trait Trait {}
|
|
|
|
impl Trait for Struct {}
|
2022-11-04 16:40:06 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-07-19 16:20:09 -05:00
|
|
|
}
|