mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Save source & target types in hir
's expr_adjustments
This commit is contained in:
parent
927d56a67d
commit
91a89efcf2
@ -3691,6 +3691,13 @@ impl From<ItemInNs> for ScopeDef {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub struct Adjustment {
|
||||||
|
pub source: Type,
|
||||||
|
pub target: Type,
|
||||||
|
pub kind: Adjust,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub enum Adjust {
|
pub enum Adjust {
|
||||||
/// Go from ! to any type.
|
/// Go from ! to any type.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
mod source_to_def;
|
mod source_to_def;
|
||||||
|
|
||||||
use std::{cell::RefCell, fmt, iter, ops};
|
use std::{cell::RefCell, fmt, iter, mem, ops};
|
||||||
|
|
||||||
use base_db::{FileId, FileRange};
|
use base_db::{FileId, FileRange};
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
@ -29,7 +29,7 @@ use crate::{
|
|||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
|
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
|
||||||
source_analyzer::{resolve_hir_path, SourceAnalyzer},
|
source_analyzer::{resolve_hir_path, SourceAnalyzer},
|
||||||
Access, Adjust, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
|
Access, Adjust, Adjustment, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
|
||||||
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
|
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
|
||||||
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, ToolModule, Trait, Type,
|
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, ToolModule, Trait, Type,
|
||||||
TypeAlias, TypeParam, VariantDef,
|
TypeAlias, TypeParam, VariantDef,
|
||||||
@ -334,7 +334,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
|||||||
self.imp.resolve_trait(trait_)
|
self.imp.resolve_trait(trait_)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
|
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
|
||||||
self.imp.expr_adjustments(expr)
|
self.imp.expr_adjustments(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1067,14 +1067,22 @@ impl<'db> SemanticsImpl<'db> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
|
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
|
||||||
let mutability = |m| match m {
|
let mutability = |m| match m {
|
||||||
hir_ty::Mutability::Not => Mutability::Shared,
|
hir_ty::Mutability::Not => Mutability::Shared,
|
||||||
hir_ty::Mutability::Mut => Mutability::Mut,
|
hir_ty::Mutability::Mut => Mutability::Mut,
|
||||||
};
|
};
|
||||||
self.analyze(expr.syntax())?.expr_adjustments(self.db, expr).map(|it| {
|
|
||||||
|
let analyzer = self.analyze(expr.syntax())?;
|
||||||
|
|
||||||
|
let (mut source_ty, _) = analyzer.type_of_expr(self.db, expr)?;
|
||||||
|
|
||||||
|
analyzer.expr_adjustments(self.db, expr).map(|it| {
|
||||||
it.iter()
|
it.iter()
|
||||||
.map(|adjust| match adjust.kind {
|
.map(|adjust| {
|
||||||
|
let target =
|
||||||
|
Type::new_with_resolver(self.db, &analyzer.resolver, adjust.target.clone());
|
||||||
|
let kind = match adjust.kind {
|
||||||
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
|
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
|
||||||
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
|
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
|
||||||
Adjust::Deref(Some(OverloadedDeref(mutability(m))))
|
Adjust::Deref(Some(OverloadedDeref(mutability(m))))
|
||||||
@ -1087,6 +1095,14 @@ impl<'db> SemanticsImpl<'db> {
|
|||||||
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
|
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
|
||||||
}
|
}
|
||||||
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
|
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update `source_ty` for the next adjustment
|
||||||
|
let source = mem::replace(&mut source_ty, target.clone());
|
||||||
|
|
||||||
|
let adjustment = Adjustment { source, target, kind };
|
||||||
|
|
||||||
|
adjustment
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
|
@ -60,7 +60,7 @@ pub(super) fn hints(
|
|||||||
}
|
}
|
||||||
for adjustment in adjustments.into_iter().rev() {
|
for adjustment in adjustments.into_iter().rev() {
|
||||||
// FIXME: Add some nicer tooltips to each of these
|
// FIXME: Add some nicer tooltips to each of these
|
||||||
let text = match adjustment {
|
let text = match adjustment.kind {
|
||||||
Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
|
Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
|
||||||
"<never-to-any>"
|
"<never-to-any>"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user