Skip to content

Commit 7c8c6d2

Browse files
committed
Semantic changes from new hir representation
Always lower to `GenericArg::Infer` Update `PlaceholderCollector` Update closure lifetime binder infer var visitor Fallback visitor handle ambig infer args Ensure type infer args have their type recorded
1 parent 98d80e2 commit 7c8c6d2

File tree

7 files changed

+47
-53
lines changed

7 files changed

+47
-53
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10851085
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
10861086
ast::GenericArg::Type(ty) => {
10871087
match &ty.kind {
1088-
TyKind::Infer if self.tcx.features().generic_arg_infer() => {
1088+
TyKind::Infer => {
10891089
return GenericArg::Infer(hir::InferArg {
10901090
hir_id: self.lower_node_id(ty.id),
10911091
span: self.lower_span(ty.span),

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,29 +139,12 @@ pub(crate) struct HirPlaceholderCollector {
139139
}
140140

141141
impl<'v> Visitor<'v> for HirPlaceholderCollector {
142-
fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
143-
if let hir::TyKind::Infer = t.kind {
144-
self.spans.push(t.span);
145-
}
146-
intravisit::walk_ty(self, t)
147-
}
148-
fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) {
149-
match generic_arg {
150-
hir::GenericArg::Infer(inf) => {
151-
self.spans.push(inf.span);
152-
self.may_contain_const_infer = true;
153-
intravisit::walk_inf(self, inf);
154-
}
155-
hir::GenericArg::Type(t) => self.visit_ty(t),
156-
_ => {}
157-
}
158-
}
159-
fn visit_const_arg(&mut self, const_arg: &'v hir::ConstArg<'v>) {
160-
if let hir::ConstArgKind::Infer(span) = const_arg.kind {
142+
fn visit_infer(&mut self, _inf_id: HirId, inf_span: Span, kind: InferKind<'v>) -> Self::Result {
143+
self.spans.push(inf_span);
144+
145+
if let InferKind::Const(_) | InferKind::Ambig(_) = kind {
161146
self.may_contain_const_infer = true;
162-
self.spans.push(span);
163147
}
164-
intravisit::walk_const_arg(self, const_arg)
165148
}
166149
}
167150

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,17 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
489489
struct FindInferInClosureWithBinder;
490490
impl<'v> Visitor<'v> for FindInferInClosureWithBinder {
491491
type Result = ControlFlow<Span>;
492-
fn visit_ty(&mut self, t: &'v hir::Ty<'v>) -> Self::Result {
493-
if matches!(t.kind, hir::TyKind::Infer) {
494-
ControlFlow::Break(t.span)
495-
} else {
496-
intravisit::walk_ty(self, t)
497-
}
492+
493+
fn visit_infer(
494+
&mut self,
495+
_inf_id: HirId,
496+
inf_span: Span,
497+
_kind: InferKind<'v>,
498+
) -> Self::Result {
499+
ControlFlow::Break(inf_span)
498500
}
499501
}
500-
FindInferInClosureWithBinder.visit_ty(ty).break_value()
502+
FindInferInClosureWithBinder.visit_unambig_ty(ty).break_value()
501503
}
502504

503505
let infer_in_rt_sp = match fn_decl.output {

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ fn generic_arg_mismatch_err(
4040
param.kind.descr(),
4141
);
4242

43-
if let GenericParamDefKind::Const { .. } = param.kind {
44-
if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Infer, .. })) {
45-
err.help("const arguments cannot yet be inferred with `_`");
46-
tcx.disabled_nightly_features(
47-
&mut err,
48-
param.def_id.as_local().map(|local| tcx.local_def_id_to_hir_id(local)),
49-
[(String::new(), sym::generic_arg_infer)],
50-
);
51-
}
52-
}
53-
5443
let add_braces_suggestion = |arg: &GenericArg<'_>, err: &mut Diag<'_>| {
5544
let suggestions = vec![
5645
(arg.span().shrink_to_lo(), String::from("{ ")),
@@ -269,6 +258,21 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
269258
GenericParamDefKind::Const { .. },
270259
_,
271260
) => {
261+
if let GenericParamDefKind::Const { .. } = param.kind
262+
&& let GenericArg::Infer(inf) = arg
263+
&& !tcx.features().generic_arg_infer()
264+
{
265+
rustc_session::parse::feature_err(
266+
tcx.sess,
267+
sym::generic_arg_infer,
268+
inf.span,
269+
"const arguments cannot yet be inferred with `_`",
270+
)
271+
.emit();
272+
}
273+
274+
// We lower to an infer even when the feature gate is not enabled
275+
// as it is useful for diagnostics to be able to see a `ConstKind::Infer`
272276
args.push(ctx.provided_kind(&args, param, arg));
273277
args_iter.next();
274278
params.next();

compiler/rustc_hir_typeck/src/fallback.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir as hir;
1010
use rustc_hir::HirId;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::DefId;
13-
use rustc_hir::intravisit::Visitor;
13+
use rustc_hir::intravisit::{InferKind, Visitor};
1414
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
1515
use rustc_session::lint;
1616
use rustc_span::def_id::LocalDefId;
@@ -641,16 +641,21 @@ impl<'tcx> AnnotateUnitFallbackVisitor<'_, 'tcx> {
641641
impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
642642
type Result = ControlFlow<errors::SuggestAnnotation>;
643643

644-
fn visit_ty(&mut self, hir_ty: &'tcx hir::Ty<'tcx>) -> Self::Result {
644+
fn visit_infer(
645+
&mut self,
646+
inf_id: HirId,
647+
inf_span: Span,
648+
_kind: InferKind<'tcx>,
649+
) -> Self::Result {
645650
// Try to replace `_` with `()`.
646-
if let hir::TyKind::Infer = hir_ty.kind
647-
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(hir_ty.hir_id)
651+
if let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(inf_id)
648652
&& let Some(vid) = self.fcx.root_vid(ty)
649653
&& self.reachable_vids.contains(&vid)
650654
{
651-
return ControlFlow::Break(errors::SuggestAnnotation::Unit(hir_ty.span));
655+
return ControlFlow::Break(errors::SuggestAnnotation::Unit(inf_span));
652656
}
653-
hir::intravisit::walk_ty(self, hir_ty)
657+
658+
ControlFlow::Continue(())
654659
}
655660

656661
fn visit_qpath(

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,17 +1272,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12721272
.lower_lifetime(lt, RegionInferReason::Param(param))
12731273
.into(),
12741274
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
1275-
// We handle the ambig portions of `Ty` in match arms below
1275+
// We handle the ambig portions of `Ty` in match arm below
12761276
self.fcx.lower_ty(ty.as_unambig_ty()).raw.into()
12771277
}
1278+
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1279+
self.fcx.lower_ty(&inf.to_ty()).raw.into()
1280+
}
12781281
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
12791282
.fcx
12801283
// Ambiguous parts of `ConstArg` are handled in the match arms below
12811284
.lower_const_arg(ct.as_unambig_ct(), FeedConstTy::Param(param.def_id))
12821285
.into(),
1283-
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
1284-
self.fcx.ty_infer(Some(param), inf.span).into()
1285-
}
12861286
(&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
12871287
self.fcx.ct_infer(Some(param), inf.span).into()
12881288
}

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,14 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
428428
// We handle the ambig portions of `Ty` in the match arms below
429429
self.cfcx.lower_ty(ty.as_unambig_ty()).raw.into()
430430
}
431+
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
432+
self.cfcx.lower_ty(&inf.to_ty()).raw.into()
433+
}
431434
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
432435
.cfcx
433436
// We handle the ambig portions of `ConstArg` in the match arms below
434437
.lower_const_arg(ct.as_unambig_ct(), FeedConstTy::Param(param.def_id))
435438
.into(),
436-
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
437-
self.cfcx.ty_infer(Some(param), inf.span).into()
438-
}
439439
(GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
440440
self.cfcx.ct_infer(Some(param), inf.span).into()
441441
}

0 commit comments

Comments
 (0)