Skip to content

Commit 3605675

Browse files
committed
Add inferred args to typeck
1 parent 417b098 commit 3605675

File tree

18 files changed

+132
-52
lines changed

18 files changed

+132
-52
lines changed

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ pub trait Visitor<'v>: Sized {
437437
walk_label(self, label)
438438
}
439439
fn visit_infer(&mut self, inf: &'v InferArg) {
440-
self.visit_id(inf.hir_id);
440+
walk_inf(self, inf);
441441
}
442442
fn visit_generic_arg(&mut self, generic_arg: &'v GenericArg<'v>) {
443443
match generic_arg {
@@ -447,11 +447,6 @@ pub trait Visitor<'v>: Sized {
447447
GenericArg::Infer(inf) => self.visit_infer(inf),
448448
}
449449
}
450-
/*
451-
fn tcx(&self) -> Option<&TyCtxt<'tcx>> {
452-
None
453-
}
454-
*/
455450
fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
456451
walk_lifetime(self, lifetime)
457452
}

compiler/rustc_lint/src/late.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
244244
hir_visit::walk_ty(self, t);
245245
}
246246

247+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
248+
lint_callback!(self, check_infer, inf);
249+
hir_visit::walk_inf(self, inf);
250+
}
251+
247252
fn visit_name(&mut self, sp: Span, name: Symbol) {
248253
lint_callback!(self, check_name, sp, name);
249254
}

compiler/rustc_lint/src/passes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ macro_rules! late_lint_methods {
3333
fn check_expr(a: &$hir hir::Expr<$hir>);
3434
fn check_expr_post(a: &$hir hir::Expr<$hir>);
3535
fn check_ty(a: &$hir hir::Ty<$hir>);
36+
fn check_infer(a: &$hir hir::InferArg);
3637
fn check_generic_arg(a: &$hir hir::GenericArg<$hir>);
3738
fn check_generic_param(a: &$hir hir::GenericParam<$hir>);
3839
fn check_generics(a: &$hir hir::Generics<$hir>);

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,11 @@ impl<'hir> Map<'hir> {
272272
GenericParamKind::Type { .. } => DefKind::TyParam,
273273
GenericParamKind::Const { .. } => DefKind::ConstParam,
274274
},
275-
Node::Infer(_) => todo!(),
276275
Node::Crate(_) => DefKind::Mod,
277276
Node::Stmt(_)
278277
| Node::PathSegment(_)
279278
| Node::Ty(_)
279+
| Node::Infer(_)
280280
| Node::TraitRef(_)
281281
| Node::Pat(_)
282282
| Node::Binding(_)

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ rustc_queries! {
133133
cache_on_disk_if { key.is_local() }
134134
}
135135

136-
query generic_arg_for_infer_arg(key: DefId) -> hir::GenericArg<'tcx> {
137-
desc { |tcx| "computes concrete type for inference, `{}`", tcx.def_path_str(key) }
138-
storage(ArenaCacheSelector<'tcx>)
139-
}
140-
141136
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
142137
/// predicates (where-clauses) that must be proven true in order
143138
/// to reference it. This is almost always the "predicates query"

compiler/rustc_privacy/src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,16 +1191,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
11911191
fn visit_generic_arg(&mut self, generic_arg: &'tcx hir::GenericArg<'tcx>) {
11921192
match generic_arg {
11931193
hir::GenericArg::Type(t) => self.visit_ty(t),
1194-
hir::GenericArg::Infer(inf) => {
1195-
self.span = inf.span;
1196-
let parent_hir_id = self.tcx.hir().get_parent_node(inf.hir_id);
1197-
if let Some(typeck_results) = self.maybe_typeck_results {
1198-
let node_substs = typeck_results.node_substs(parent_hir_id);
1199-
for ty in node_substs.types() {
1200-
self.visit(ty);
1201-
}
1202-
}
1203-
}
1194+
hir::GenericArg::Infer(inf) => self.visit_infer(inf),
12041195
hir::GenericArg::Lifetime(_) | hir::GenericArg::Const(_) => {}
12051196
}
12061197
}
@@ -1224,6 +1215,23 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12241215
intravisit::walk_ty(self, hir_ty);
12251216
}
12261217

1218+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
1219+
self.span = inf.span;
1220+
if let Some(typeck_results) = self.maybe_typeck_results {
1221+
if let Some(ty) = typeck_results.node_type_opt(inf.hir_id) {
1222+
if self.visit(ty).is_break() {
1223+
return;
1224+
}
1225+
}
1226+
} else {
1227+
// FIXME see above note for same issue.
1228+
if self.visit(rustc_typeck::hir_ty_to_ty(self.tcx, &inf.to_ty())).is_break() {
1229+
return;
1230+
}
1231+
}
1232+
intravisit::walk_inf(self, inf);
1233+
}
1234+
12271235
fn visit_trait_ref(&mut self, trait_ref: &'tcx hir::TraitRef<'tcx>) {
12281236
self.span = trait_ref.path.span;
12291237
if self.maybe_typeck_results.is_none() {

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS;
1818
use rustc_span::{symbol::kw, MultiSpan, Span};
1919
use smallvec::SmallVec;
2020

21-
/*
22-
pub fn generic_arg_for_infer_arg<'tcx>(tcx: TyCtxt<'tcx>, did: LocalDefId) -> GenericArg<'tcx> {
23-
todo!()
24-
let hir_id = tcx.hir().local_def_id_to_hir_id(did);
25-
let arg = match tcx.hir().get(hir_id) {
26-
hir::Node::GenericParam(hir::GenericParam {
27-
kind: hir::GenericParamKind::Const { ty: _, default: _ },
28-
..
29-
}) => todo!(),
30-
_ => bug!("Expected GenericParam for generic_arg_for_infer_arg"),
31-
};
32-
33-
assert!(!matches!(arg, GenericArg::Infer(_)));
34-
arg
35-
}
36-
*/
37-
3821
impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
3922
/// Report an error that a generic argument did not match the generic parameter that was
4023
/// expected.

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
479479
param.def_id,
480480
Some(arg.id()),
481481
arg.span(),
482+
None,
482483
|_, _| {
483484
// Default generic parameters may not be marked
484485
// with stability attributes, i.e. when the

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
581581
}
582582
}
583583

584+
pub fn node_ty_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
585+
match self.typeck_results.borrow().node_types().get(id) {
586+
Some(&t) => Some(t),
587+
None if self.is_tainted_by_errors() => Some(self.tcx.ty_error()),
588+
None => None,
589+
}
590+
}
591+
584592
/// Registers an obligation for checking later, during regionck, that `arg` is well-formed.
585593
pub fn register_wf_obligation(
586594
&self,

compiler/rustc_typeck/src/check/writeback.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
331331
let ty = self.resolve(ty, &hir_ty.span);
332332
self.write_ty_to_typeck_results(hir_ty.hir_id, ty);
333333
}
334+
335+
fn visit_infer(&mut self, inf: &'tcx hir::InferArg) {
336+
intravisit::walk_inf(self, inf);
337+
// Ignore cases where the inference is a const.
338+
if let Some(ty) = self.fcx.node_ty_opt(inf.hir_id) {
339+
let ty = self.resolve(ty, &inf.span);
340+
self.write_ty_to_typeck_results(inf.hir_id, ty);
341+
}
342+
}
334343
}
335344

336345
impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {

0 commit comments

Comments
 (0)