Skip to content

Commit b96f1ad

Browse files
committed
Give TypeInfo fields and methods more appropriate names
1 parent 8afa272 commit b96f1ad

30 files changed

+76
-74
lines changed

crates/hir/src/semantics.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,23 @@ impl PathResolution {
9090
#[derive(Debug)]
9191
pub struct TypeInfo {
9292
/// The original type of the expression or pattern.
93-
pub ty: Type,
94-
/// The coerced type, if a coercion happened.
95-
pub coerced: Option<Type>,
93+
pub original: Type,
94+
/// The adjusted type, if an adjustment happened.
95+
pub adjusted: Option<Type>,
9696
}
9797

9898
impl TypeInfo {
99-
pub fn ty(self) -> Type {
100-
self.ty
99+
pub fn original(self) -> Type {
100+
self.original
101101
}
102102

103-
pub fn has_coercion(&self) -> bool {
104-
self.coerced.is_some()
103+
pub fn has_adjustment(&self) -> bool {
104+
self.adjusted.is_some()
105105
}
106106

107-
pub fn coerced(self) -> Type {
108-
self.coerced.unwrap_or(self.ty)
107+
/// The adjusted type, or the original in case no adjustments occurred.
108+
pub fn adjusted(self) -> Type {
109+
self.adjusted.unwrap_or(self.original)
109110
}
110111
}
111112

@@ -581,13 +582,13 @@ impl<'db> SemanticsImpl<'db> {
581582
fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
582583
self.analyze(expr.syntax())
583584
.type_of_expr(self.db, expr)
584-
.map(|(ty, coerced)| TypeInfo { ty, coerced })
585+
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
585586
}
586587

587588
fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
588589
self.analyze(pat.syntax())
589590
.type_of_pat(self.db, pat)
590-
.map(|(ty, coerced)| TypeInfo { ty, coerced })
591+
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
591592
}
592593

593594
fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
@@ -766,7 +767,7 @@ impl<'db> SemanticsImpl<'db> {
766767
ast::Expr::FieldExpr(field_expr) => field_expr,
767768
_ => return None,
768769
};
769-
let ty = self.type_of_expr(&field_expr.expr()?)?.ty;
770+
let ty = self.type_of_expr(&field_expr.expr()?)?.original;
770771
if !ty.is_packed(self.db) {
771772
return None;
772773
}
@@ -793,7 +794,7 @@ impl<'db> SemanticsImpl<'db> {
793794
self.type_of_expr(&expr)
794795
})
795796
// Binding a reference to a packed type is possibly unsafe.
796-
.map(|ty| ty.ty.is_packed(self.db))
797+
.map(|ty| ty.original.is_packed(self.db))
797798
.unwrap_or(false)
798799

799800
// FIXME This needs layout computation to be correct. It will highlight
@@ -839,7 +840,7 @@ impl<'db> SemanticsImpl<'db> {
839840
}
840841
})
841842
// Binding a reference to a packed type is possibly unsafe.
842-
.map(|ty| ty.ty.is_packed(self.db))
843+
.map(|ty| ty.original.is_packed(self.db))
843844
.unwrap_or(false)
844845
}
845846
}

crates/ide/src/call_hierarchy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
8686
let name_ref = call_node.name_ref()?;
8787
let func_target = match call_node {
8888
FnCallNode::CallExpr(expr) => {
89-
let callable = sema.type_of_expr(&expr.expr()?)?.ty.as_callable(db)?;
89+
let callable = sema.type_of_expr(&expr.expr()?)?.original.as_callable(db)?;
9090
match callable.kind() {
9191
hir::CallableKind::Function(it) => it.try_to_nav(db),
9292
_ => None,

crates/ide/src/goto_type_definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub(crate) fn goto_type_definition(
3232
let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| {
3333
let ty = match_ast! {
3434
match node {
35-
ast::Expr(it) => sema.type_of_expr(&it)?.ty,
36-
ast::Pat(it) => sema.type_of_pat(&it)?.ty,
35+
ast::Expr(it) => sema.type_of_expr(&it)?.original,
36+
ast::Pat(it) => sema.type_of_pat(&it)?.original,
3737
ast::SelfParam(it) => sema.type_of_self(&it)?,
3838
ast::Type(it) => sema.resolve_type(&it)?,
3939
ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?,

crates/ide/src/highlight_related.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn highlight_exit_points(
123123
}
124124
}
125125
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
126-
if sema.type_of_expr(&expr).map_or(false, |ty| ty.ty.is_never()) {
126+
if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
127127
highlights
128128
.push(HighlightedRange { access: None, range: expr.syntax().text_range() });
129129
}

crates/ide/src/hover.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,29 +225,29 @@ fn hover_type_info(
225225
config: &HoverConfig,
226226
expr_or_pat: &Either<ast::Expr, ast::Pat>,
227227
) -> Option<HoverResult> {
228-
let TypeInfo { ty, coerced } = match expr_or_pat {
228+
let TypeInfo { original, adjusted } = match expr_or_pat {
229229
Either::Left(expr) => sema.type_of_expr(expr)?,
230230
Either::Right(pat) => sema.type_of_pat(pat)?,
231231
};
232232

233233
let mut res = HoverResult::default();
234-
res.markup = if let Some(coerced_ty) = coerced {
235-
let uncoerced = ty.display(sema.db).to_string();
236-
let coerced = coerced_ty.display(sema.db).to_string();
234+
res.markup = if let Some(adjusted_ty) = adjusted {
235+
let original = original.display(sema.db).to_string();
236+
let adjusted = adjusted_ty.display(sema.db).to_string();
237237
format!(
238-
"```text\nType: {:>upad$}\nCoerced to: {:>cpad$}\n```\n",
239-
uncoerced = uncoerced,
240-
coerced = coerced,
238+
"```text\nType: {:>apad$}\nCoerced to: {:>opad$}\n```\n",
239+
uncoerced = original,
240+
coerced = adjusted,
241241
// 6 base padding for static text prefix of each line
242-
upad = 6 + coerced.len().max(uncoerced.len()),
243-
cpad = uncoerced.len(),
242+
apad = 6 + adjusted.len().max(original.len()),
243+
opad = original.len(),
244244
)
245245
.into()
246246
} else {
247247
if config.markdown() {
248-
Markup::fenced_block(&ty.display(sema.db))
248+
Markup::fenced_block(&original.display(sema.db))
249249
} else {
250-
ty.display(sema.db).to_string().into()
250+
original.display(sema.db).to_string().into()
251251
}
252252
};
253253
Some(res)

crates/ide/src/inlay_hints.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn get_chaining_hints(
117117
next_next = tokens.next()?.kind();
118118
}
119119
if next_next == T![.] {
120-
let ty = sema.type_of_expr(&expr)?.ty;
120+
let ty = sema.type_of_expr(&expr)?.original;
121121
if ty.is_unknown() {
122122
return None;
123123
}
@@ -189,7 +189,7 @@ fn get_bind_pat_hints(
189189
let krate = sema.scope(pat.syntax()).module().map(|it| it.krate());
190190
let famous_defs = FamousDefs(sema, krate);
191191

192-
let ty = sema.type_of_pat(&pat.clone().into())?.ty;
192+
let ty = sema.type_of_pat(&pat.clone().into())?.original;
193193

194194
if should_not_display_type_hint(sema, &pat, &ty) {
195195
return None;
@@ -308,7 +308,7 @@ fn should_not_display_type_hint(
308308
return it.in_token().is_none() ||
309309
it.iterable()
310310
.and_then(|iterable_expr| sema.type_of_expr(&iterable_expr))
311-
.map(TypeInfo::ty)
311+
.map(TypeInfo::original)
312312
.map_or(true, |iterable_ty| iterable_ty.is_unknown() || iterable_ty.is_unit())
313313
},
314314
_ => (),
@@ -394,7 +394,7 @@ fn is_enum_name_similar_to_param_name(
394394
argument: &ast::Expr,
395395
param_name: &str,
396396
) -> bool {
397-
match sema.type_of_expr(argument).and_then(|t| t.ty.as_adt()) {
397+
match sema.type_of_expr(argument).and_then(|t| t.original.as_adt()) {
398398
Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name,
399399
_ => false,
400400
}
@@ -431,7 +431,7 @@ fn get_callable(
431431
) -> Option<(hir::Callable, ast::ArgList)> {
432432
match expr {
433433
ast::Expr::CallExpr(expr) => {
434-
sema.type_of_expr(&expr.expr()?)?.ty.as_callable(sema.db).zip(expr.arg_list())
434+
sema.type_of_expr(&expr.expr()?)?.original.as_callable(sema.db).zip(expr.arg_list())
435435
}
436436
ast::Expr::MethodCallExpr(expr) => {
437437
sema.resolve_method_call_as_callable(expr).zip(expr.arg_list())

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub(super) fn element(
123123
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
124124

125125
let expr = prefix_expr.expr()?;
126-
let ty = sema.type_of_expr(&expr)?.ty;
126+
let ty = sema.type_of_expr(&expr)?.original;
127127
if ty.is_raw_ptr() {
128128
HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
129129
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
@@ -555,7 +555,7 @@ fn highlight_method_call(
555555
if let Some(receiver_ty) =
556556
method_call.receiver().and_then(|it| sema.type_of_expr(&it))
557557
{
558-
if !receiver_ty.coerced().is_copy(sema.db) {
558+
if !receiver_ty.adjusted().is_copy(sema.db) {
559559
h |= HlMod::Consuming
560560
}
561561
}

crates/ide_assists/src/handlers/add_explicit_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio
5757
(ast::Pat::IdentPat(_), Some(expr)) => ctx.sema.type_of_expr(&expr)?,
5858
(pat, _) => ctx.sema.type_of_pat(&pat)?,
5959
}
60-
.coerced();
60+
.adjusted();
6161

6262
// Unresolved or unnameable types can't be annotated
6363
if ty.contains_unknown() || ty.is_closure() {

crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn validate_method_call_expr(
8686
let receiver = expr.receiver()?;
8787
let expr = ast::Expr::MethodCallExpr(expr);
8888

89-
let it_type = sema.type_of_expr(&receiver)?.coerced();
89+
let it_type = sema.type_of_expr(&receiver)?.adjusted();
9090
let module = sema.scope(receiver.syntax()).module()?;
9191
let krate = module.krate();
9292

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl FlowKind {
345345
FlowKind::Return(Some(expr))
346346
| FlowKind::Break(Some(expr))
347347
| FlowKind::TryReturn { expr, .. } => {
348-
ctx.sema.type_of_expr(expr).map(TypeInfo::coerced)
348+
ctx.sema.type_of_expr(expr).map(TypeInfo::adjusted)
349349
}
350350
FlowKind::Try { .. } => {
351351
stdx::never!("try does not have defined expr_ty");
@@ -852,7 +852,7 @@ fn either_syntax(value: &Either<ast::IdentPat, ast::SelfParam>) -> &SyntaxNode {
852852

853853
fn body_return_ty(ctx: &AssistContext, body: &FunctionBody) -> Option<RetType> {
854854
match body.tail_expr() {
855-
Some(expr) => ctx.sema.type_of_expr(&expr).map(TypeInfo::ty).map(RetType::Expr),
855+
Some(expr) => ctx.sema.type_of_expr(&expr).map(TypeInfo::original).map(RetType::Expr),
856856
None => Some(RetType::Stmt),
857857
}
858858
}
@@ -949,7 +949,7 @@ fn expr_err_kind(expr: &ast::Expr, ctx: &AssistContext) -> Option<TryKind> {
949949
let text = func_name.syntax().text();
950950

951951
if text == "Err" {
952-
Some(TryKind::Result { ty: ctx.sema.type_of_expr(expr).map(TypeInfo::ty)? })
952+
Some(TryKind::Result { ty: ctx.sema.type_of_expr(expr).map(TypeInfo::original)? })
953953
} else if text == "None" {
954954
Some(TryKind::Option)
955955
} else {

0 commit comments

Comments
 (0)