Skip to content

Commit 3179126

Browse files
authored
Rollup merge of #113187 - compiler-errors:local-ty, r=b-naber
No need to distinguish `LocalTy` from `Ty` I think the distinction between `decl_ty` and `revealed_ty` was from when you were allowed to put `impl Trait` in let bindings... I don't think we need that anymore, and it makes typeck that much more confusing 😆 Side-note: I don't know why we store this in a separate field [`locals`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/struct.Inherited.html#structfield.locals) in `Inherited`, rather than just the `TypeckResults`... Might look into changing that next.
2 parents 1f13953 + 91bc3f0 commit 3179126

File tree

7 files changed

+22
-34
lines changed

7 files changed

+22
-34
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::callee::{self, DeferredCallResolution};
22
use crate::errors::CtorIsPrivate;
33
use crate::method::{self, MethodCallee, SelfSource};
44
use crate::rvalue_scopes;
5-
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, RawTy};
5+
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, RawTy};
66
use rustc_data_structures::captures::Captures;
77
use rustc_data_structures::fx::FxHashSet;
88
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, MultiSpan, StashKey};
@@ -135,7 +135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
135135
format!("{:p}", self)
136136
}
137137

138-
pub fn local_ty(&self, span: Span, nid: hir::HirId) -> LocalTy<'tcx> {
138+
pub fn local_ty(&self, span: Span, nid: hir::HirId) -> Ty<'tcx> {
139139
self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
140140
span_bug!(span, "no type for local variable {}", self.tcx.hir().node_to_string(nid))
141141
})
@@ -1152,7 +1152,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11521152
);
11531153

11541154
if let Res::Local(hid) = res {
1155-
let ty = self.local_ty(span, hid).decl_ty;
1155+
let ty = self.local_ty(span, hid);
11561156
let ty = self.normalize(span, ty);
11571157
self.write_ty(hir_id, ty);
11581158
return (ty, res);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::method::MethodCallee;
66
use crate::TupleArgumentsFlag::*;
77
use crate::{errors, Expectation::*};
88
use crate::{
9-
struct_span_err, BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, Needs, RawTy,
10-
TupleArgumentsFlag,
9+
struct_span_err, BreakableCtxt, Diverges, Expectation, FnCtxt, Needs, RawTy, TupleArgumentsFlag,
1110
};
1211
use rustc_ast as ast;
1312
use rustc_data_structures::fx::FxIndexSet;
@@ -1423,7 +1422,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14231422
// See #44848.
14241423
let ref_bindings = pat.contains_explicit_ref_binding();
14251424

1426-
let local_ty = self.local_ty(init.span, hir_id).revealed_ty;
1425+
let local_ty = self.local_ty(init.span, hir_id);
14271426
if let Some(m) = ref_bindings {
14281427
// Somewhat subtle: if we have a `ref` binding in the pattern,
14291428
// we want to avoid introducing coercions for the RHS. This is
@@ -1453,7 +1452,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14531452

14541453
pub(in super::super) fn check_decl(&self, decl: Declaration<'tcx>) {
14551454
// Determine and write the type which we'll check the pattern against.
1456-
let decl_ty = self.local_ty(decl.span, decl.hir_id).decl_ty;
1455+
let decl_ty = self.local_ty(decl.span, decl.hir_id);
14571456
self.write_ty(decl.hir_id, decl_ty);
14581457

14591458
// Type check the initializer.
@@ -1799,9 +1798,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17991798
let err = self.tcx.ty_error(guar);
18001799
self.write_ty(hir_id, err);
18011800
self.write_ty(pat.hir_id, err);
1802-
let local_ty = LocalTy { decl_ty: err, revealed_ty: err };
1803-
self.locals.borrow_mut().insert(hir_id, local_ty);
1804-
self.locals.borrow_mut().insert(pat.hir_id, local_ty);
1801+
self.locals.borrow_mut().insert(hir_id, err);
1802+
self.locals.borrow_mut().insert(pat.hir_id, err);
18051803
}
18061804
}
18071805

compiler/rustc_hir_typeck/src/gather_locals.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{FnCtxt, LocalTy};
1+
use crate::FnCtxt;
22
use rustc_hir as hir;
33
use rustc_hir::intravisit::{self, Visitor};
44
use rustc_hir::PatKind;
@@ -48,31 +48,28 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
4848
Self { fcx, outermost_fn_param_pat: None }
4949
}
5050

51-
fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<LocalTy<'tcx>>) -> Ty<'tcx> {
51+
fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<Ty<'tcx>>) -> Ty<'tcx> {
5252
match ty_opt {
5353
None => {
5454
// Infer the variable's type.
5555
let var_ty = self.fcx.next_ty_var(TypeVariableOrigin {
5656
kind: TypeVariableOriginKind::TypeInference,
5757
span,
5858
});
59-
self.fcx
60-
.locals
61-
.borrow_mut()
62-
.insert(nid, LocalTy { decl_ty: var_ty, revealed_ty: var_ty });
59+
self.fcx.locals.borrow_mut().insert(nid, var_ty);
6360
var_ty
6461
}
6562
Some(typ) => {
6663
// Take type that the user specified.
6764
self.fcx.locals.borrow_mut().insert(nid, typ);
68-
typ.revealed_ty
65+
typ
6966
}
7067
}
7168
}
7269

73-
/// Allocates a [LocalTy] for a declaration, which may have a type annotation. If it does have
74-
/// a type annotation, then the LocalTy stored will be the resolved type. This may be found
75-
/// again during type checking by querying [FnCtxt::local_ty] for the same hir_id.
70+
/// Allocates a type for a declaration, which may have a type annotation. If it does have
71+
/// a type annotation, then the [`Ty`] stored will be the resolved type. This may be found
72+
/// again during type checking by querying [`FnCtxt::local_ty`] for the same hir_id.
7673
fn declare(&mut self, decl: Declaration<'tcx>) {
7774
let local_ty = match decl.ty {
7875
Some(ref ty) => {
@@ -87,7 +84,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
8784
.user_provided_types_mut()
8885
.insert(ty.hir_id, c_ty);
8986

90-
Some(LocalTy { decl_ty: o_ty.normalized, revealed_ty: o_ty.normalized })
87+
Some(o_ty.normalized)
9188
}
9289
None => None,
9390
};
@@ -96,7 +93,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
9693
debug!(
9794
"local variable {:?} is assigned type {}",
9895
decl.pat,
99-
self.fcx.ty_to_string(self.fcx.locals.borrow().get(&decl.hir_id).unwrap().decl_ty)
96+
self.fcx.ty_to_string(*self.fcx.locals.borrow().get(&decl.hir_id).unwrap())
10097
);
10198
}
10299
}
@@ -151,7 +148,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
151148
debug!(
152149
"pattern binding {} is assigned to {} with type {:?}",
153150
ident,
154-
self.fcx.ty_to_string(self.fcx.locals.borrow().get(&p.hir_id).unwrap().decl_ty),
151+
self.fcx.ty_to_string(*self.fcx.locals.borrow().get(&p.hir_id).unwrap()),
155152
var_ty
156153
);
157154
}

compiler/rustc_hir_typeck/src/inherited.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Inherited<'tcx> {
3030

3131
pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
3232

33-
pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
33+
pub(super) locals: RefCell<HirIdMap<Ty<'tcx>>>,
3434

3535
pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
3636

compiler/rustc_hir_typeck/src/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ macro_rules! type_error_struct {
8989
})
9090
}
9191

92-
/// The type of a local binding, including the revealed type for anon types.
93-
#[derive(Copy, Clone, Debug)]
94-
pub struct LocalTy<'tcx> {
95-
decl_ty: Ty<'tcx>,
96-
revealed_ty: Ty<'tcx>,
97-
}
98-
9992
/// If this `DefId` is a "primary tables entry", returns
10093
/// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
10194
///

compiler/rustc_hir_typeck/src/pat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
594594

595595
debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
596596

597-
let local_ty = self.local_ty(pat.span, pat.hir_id).decl_ty;
597+
let local_ty = self.local_ty(pat.span, pat.hir_id);
598598
let eq_ty = match bm {
599599
ty::BindByReference(mutbl) => {
600600
// If the binding is like `ref x | ref mut x`,
@@ -635,7 +635,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
635635
ty: Ty<'tcx>,
636636
ti: TopInfo<'tcx>,
637637
) {
638-
let var_ty = self.local_ty(span, var_id).decl_ty;
638+
let var_ty = self.local_ty(span, var_id);
639639
if let Some(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
640640
let hir = self.tcx.hir();
641641
let var_ty = self.resolve_vars_with_obligations(var_ty);

compiler/rustc_hir_typeck/src/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
348348

349349
fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
350350
intravisit::walk_local(self, l);
351-
let var_ty = self.fcx.local_ty(l.span, l.hir_id).decl_ty;
351+
let var_ty = self.fcx.local_ty(l.span, l.hir_id);
352352
let var_ty = self.resolve(var_ty, &l.span);
353353
self.write_ty_to_typeck_results(l.hir_id, var_ty);
354354
}

0 commit comments

Comments
 (0)