Skip to content

Commit b0e56ef

Browse files
committed
Auto merge of rust-lang#13545 - Veykril:adjustment-hints, r=Veykril
Generalize reborrow hints as adjustment hints Like reborrow hints, these are still mainly useful for teaching/learning ![image](https://user-images.githubusercontent.com/3757771/200073606-b5cd3b95-a9ad-454d-a3c4-d4d89bf45928.png)
2 parents 3a839ea + ee2dd93 commit b0e56ef

File tree

11 files changed

+304
-105
lines changed

11 files changed

+304
-105
lines changed

crates/hir-ty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use builder::{ParamKind, TyBuilder};
5353
pub use chalk_ext::*;
5454
pub use infer::{
5555
could_coerce, could_unify, Adjust, Adjustment, AutoBorrow, BindingMode, InferenceDiagnostic,
56-
InferenceResult,
56+
InferenceResult, OverloadedDeref, PointerCast,
5757
};
5858
pub use interner::Interner;
5959
pub use lower::{

crates/hir/src/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub use {
117117
name::{known, Name},
118118
ExpandResult, HirFileId, InFile, MacroFile, Origin,
119119
},
120-
hir_ty::display::HirDisplay,
120+
hir_ty::{display::HirDisplay, PointerCast, Safety},
121121
};
122122

123123
// These are negative re-exports: pub using these names is forbidden, they
@@ -3651,6 +3651,28 @@ impl From<ItemInNs> for ScopeDef {
36513651
}
36523652
}
36533653

3654+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3655+
pub enum Adjust {
3656+
/// Go from ! to any type.
3657+
NeverToAny,
3658+
/// Dereference once, producing a place.
3659+
Deref(Option<OverloadedDeref>),
3660+
/// Take the address and produce either a `&` or `*` pointer.
3661+
Borrow(AutoBorrow),
3662+
Pointer(PointerCast),
3663+
}
3664+
3665+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3666+
pub enum AutoBorrow {
3667+
/// Converts from T to &T.
3668+
Ref(Mutability),
3669+
/// Converts from T to *T.
3670+
RawPtr(Mutability),
3671+
}
3672+
3673+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
3674+
pub struct OverloadedDeref(pub Mutability);
3675+
36543676
pub trait HasVisibility {
36553677
fn visibility(&self, db: &dyn HirDatabase) -> Visibility;
36563678
fn is_visible_from(&self, db: &dyn HirDatabase, module: Module) -> bool {

crates/hir/src/semantics.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ use crate::{
2929
db::HirDatabase,
3030
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
3131
source_analyzer::{resolve_hir_path, SourceAnalyzer},
32-
Access, BindingMode, BuiltinAttr, Callable, ConstParam, Crate, DeriveHelper, Field, Function,
33-
HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local, Macro, Module, ModuleDef,
34-
Name, Path, ScopeDef, ToolModule, Trait, Type, TypeAlias, TypeParam, VariantDef,
32+
Access, Adjust, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
33+
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
34+
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, ToolModule, Trait, Type,
35+
TypeAlias, TypeParam, VariantDef,
3536
};
3637

3738
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -333,9 +334,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
333334
self.imp.resolve_trait(trait_)
334335
}
335336

336-
// FIXME: Figure out a nice interface to inspect adjustments
337-
pub fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
338-
self.imp.is_implicit_reborrow(expr)
337+
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
338+
self.imp.expr_adjustments(expr)
339339
}
340340

341341
pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
@@ -1067,8 +1067,29 @@ impl<'db> SemanticsImpl<'db> {
10671067
}
10681068
}
10691069

1070-
fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
1071-
self.analyze(expr.syntax())?.is_implicit_reborrow(self.db, expr)
1070+
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
1071+
let mutability = |m| match m {
1072+
hir_ty::Mutability::Not => Mutability::Shared,
1073+
hir_ty::Mutability::Mut => Mutability::Mut,
1074+
};
1075+
self.analyze(expr.syntax())?.expr_adjustments(self.db, expr).map(|it| {
1076+
it.iter()
1077+
.map(|adjust| match adjust.kind {
1078+
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
1079+
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
1080+
Adjust::Deref(Some(OverloadedDeref(mutability(m))))
1081+
}
1082+
hir_ty::Adjust::Deref(None) => Adjust::Deref(None),
1083+
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::RawPtr(m)) => {
1084+
Adjust::Borrow(AutoBorrow::RawPtr(mutability(m)))
1085+
}
1086+
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::Ref(m)) => {
1087+
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
1088+
}
1089+
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
1090+
})
1091+
.collect()
1092+
})
10721093
}
10731094

10741095
fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {

crates/hir/src/source_analyzer.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ use hir_ty::{
3838
UnsafeExpr,
3939
},
4040
method_resolution::{self, lang_names_for_bin_op},
41-
Adjust, Adjustment, AutoBorrow, InferenceResult, Interner, Substitution, Ty, TyExt, TyKind,
42-
TyLoweringContext,
41+
Adjustment, InferenceResult, Interner, Substitution, Ty, TyExt, TyKind, TyLoweringContext,
4342
};
4443
use itertools::Itertools;
4544
use smallvec::SmallVec;
@@ -156,21 +155,14 @@ impl SourceAnalyzer {
156155
Some(res)
157156
}
158157

159-
pub(crate) fn is_implicit_reborrow(
158+
pub(crate) fn expr_adjustments(
160159
&self,
161160
db: &dyn HirDatabase,
162161
expr: &ast::Expr,
163-
) -> Option<Mutability> {
162+
) -> Option<&[Adjustment]> {
164163
let expr_id = self.expr_id(db, expr)?;
165164
let infer = self.infer.as_ref()?;
166-
let adjustments = infer.expr_adjustments.get(&expr_id)?;
167-
adjustments.windows(2).find_map(|slice| match slice {
168-
&[Adjustment {kind: Adjust::Deref(None), ..}, Adjustment {kind: Adjust::Borrow(AutoBorrow::Ref(m)), ..}] => Some(match m {
169-
hir_ty::Mutability::Mut => Mutability::Mut,
170-
hir_ty::Mutability::Not => Mutability::Shared,
171-
}),
172-
_ => None,
173-
})
165+
infer.expr_adjustments.get(&expr_id).map(|v| &**v)
174166
}
175167

176168
pub(crate) fn type_of_expr(

0 commit comments

Comments
 (0)