Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions append_method.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
id: append-method-call

# rule:
# kind: call_expression
# pattern: $OBJ.instantiate($A1, $A2)
# fix: $OBJ.instantiate($A1, $A2).skip_normalization()

# rule:
# kind: call_expression
# pattern: $OBJ.instantiate_identity()
# fix: $OBJ.instantiate_identity().skip_normalization()
# rule:
# kind: call_expression
# pattern: $OBJ.instantiate_identity($A)
# fix: $OBJ.instantiate_identity($A).skip_normalization()
# rule:
# kind: call_expression
# pattern: $OBJ.instantiate_own($A1, $A2)
# fix: $OBJ.instantiate_own($A1, $A2).map(Unnormalized::skip_normalization)
# rule:
# kind: call_expression
# pattern: $OBJ.instantiate_own_identity()
# fix: $OBJ.instantiate_own_identity().map(Unnormalized::skip_normalization)
# rule:
# kind: call_expression
# pattern: $OBJ.iter_instantiated($A1, $A2)
# fix: $OBJ.iter_instantiated($A1, $A2).map(Unnormalized::skip_normalization)
# rule:
# kind: call_expression
# pattern: $OBJ.iter_identity()
# fix: $OBJ.iter_identity().map(Unnormalized::skip_normalization)

# rule:
# kind: call_expression
# pattern: $OBJ.iter_identity_copied()
# fix: $OBJ.iter_identity_copied().map(Unnormalized::skip_normalization)
# rule:
# kind: call_expression
# pattern: $OBJ.iter_instantiated_copied($A1, $A2)
# fix: $OBJ.iter_instantiated_copied($A1, $A2).map(Unnormalized::skip_normalization)

# rule:
# kind: call_expression
# pattern: $OBJ.type_of($A1).instantiate($A2, $A3)
# fix: $OBJ.type_of($A1).instantiate($A2, $A3).uncertain_skip()
# rule:
# kind: call_expression
# pattern: $OBJ.fn_sig($A1).instantiate($A2, $A3)
# fix: $OBJ.fn_sig($A1).instantiate($A2, $A3).uncertain_skip()
#
# normalization.
# rule:
# kind: call_expression
# pattern: $OBJ.normalize_erasing_regions($A1, $A2)
# fix: $OBJ.normalize_erasing_regions($A1, Unnormalized::new($A2))

# rule:
# kind: call_expression
# pattern: $OBJ.try_normalize_erasing_regions($A1, $A2)
# fix: $OBJ.try_normalize_erasing_regions($A1, Unnormalized::new($A2))

# rule:
# kind: call_expression
# pattern: wfcx.deeply_normalize($SPAN, $A2, $V)
# fix: wfcx.deeply_normalize($SPAN, $A2, Unnormalized::new($V))
#
# rule:
# kind: call_expression
# pattern: $OBJ.normalize($SPAN, $A2, $V)
# fix: $OBJ.normalize($SPAN, $A2, Unnormalized::new($V))
# borrowck normalization
# rule:
# kind: call_expression
# pattern: $OBJ.normalize($A1, $A2)
# fix: $OBJ.normalize(Unnormalized::new($A1), $A2)
# rule:
# kind: call_expression
# pattern: $OBJ.deeply_normalize($A1, $A2)
# fix: $OBJ.deeply_normalize(Unnormalized::new($A1), $A2)

# rule:
# kind: call_expression
# pattern: DeeplyNormalize { value: $V }
# fix: DeeplyNormalize { value: Unnormalized::new($V), tcx: PhantomData }

# uncertain skip iter
# rule:
# kind: call_expression
# pattern: $OBJ.iter_instantiated_copied($A1, $A2)
# fix: $OBJ.iter_instantiated_copied($A1, $A2).map(Unnormalized::uncertain_skip)


# rule:
# kind: call_expression
# pattern: $OBJ.ty($A1, args)
# fix: $OBJ.ty($A1, args).uncertain_skip()
# rule:
# kind: call_expression
# pattern: ty::EarlyBinder::bind($A1).instantiate($A2, $A3)
# fix: ty::EarlyBinder::bind($A1).instantiate($A2, $A3).uncertain_skip()

# rule:
# kind: call_expression
# pattern: $OBJ.normalize($A1, $A2, $A3)
# fix: ocx.structurally_normalize_ty($A1, $A2, Unnormalized::new($A3))
#
# rule:
# kind: call_expression
# pattern: $OBJ.instantiate_identity().skip_normalization()
# fix: $OBJ.instantiate_identity()
rule:
kind: call_expression
# pattern: $OBJ.instantiate($A1, $A2).skip_normalization()
pattern: $OBJ.instantiate($$$ARGS)
# fix: $OBJ.instantiate($A1, $A2)
# rule:
# kind: call_expression
# pattern: $OBJ.iter_identity().map(Unnormalized::skip_normalization)
# fix: $OBJ.iter_identity()
# rule:
# kind: call_expression
# pattern: $OBJ.iter_instantiated_copied($A1, $A2).map(Unnormalized::skip_normalization)
# fix: $OBJ.iter_instantiated_copied($A1, $A2)
# rule:
# kind: call_expression
# pattern: $OBJ.iter_instantiated($A1, $A2).map(Unnormalized::skip_normalization)
# fix: $OBJ.iter_instantiated($A1, $A2)

language: Rust
64 changes: 41 additions & 23 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// The move occurred as one of the arguments to a function call. Is that
// argument generic? `def_id` can't be a closure here, so using `fn_sig` is fine
let arg_param = if self.infcx.tcx.def_kind(def_id).is_fn_like()
&& let sig =
self.infcx.tcx.fn_sig(def_id).instantiate_identity().skip_binder()
&& let sig = self
.infcx
.tcx
.fn_sig(def_id)
.instantiate_identity()
.skip_normalization()
.skip_binder()
Comment on lines +480 to +481
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

can you add fn skip_binder to Unnormalized<[Early]Binder<T>> directly. When skipping binders we also don't care about normalization

&& let Some(arg_ty) = sig.inputs().get(pos + offset)
&& let ty::Param(arg_param) = arg_ty.kind()
{
Expand Down Expand Up @@ -685,7 +690,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
place_span: Span,
) -> Option<ty::Mutability> {
let tcx = self.infcx.tcx;
let sig = tcx.fn_sig(callee_did).instantiate_identity().skip_binder();
let sig = tcx.fn_sig(callee_did).instantiate_identity().skip_normalization().skip_binder();
let clauses = tcx.predicates_of(callee_did);

let generic_args = match call_expr.kind {
Expand All @@ -703,7 +708,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

// First, is there at least one method on one of `param`'s trait bounds?
// This keeps us from suggesting borrowing the argument to `mem::drop`, e.g.
if !clauses.instantiate_identity(tcx).predicates.iter().any(|clause| {
if !clauses.instantiate_identity(tcx).skip_normalization().predicates.iter().any(|clause| {
clause.as_trait_clause().is_some_and(|tc| {
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

we probably want a function fn as_trait_clause on Unnormalized to return Unnormalized<Binder<PolyTraitClause>> and duplicate a bunch of the field projections we already have for binder, instead also returning Unnormalized` of the field/wrapped thing

tc.self_ty().skip_binder().is_param(param.index)
&& tc.polarity() == ty::PredicatePolarity::Positive
Expand All @@ -729,8 +734,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
));
let can_subst = |ty: Ty<'tcx>| {
// Normalize before comparing to see through type aliases and projections.
let old_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args);
let new_ty = ty::EarlyBinder::bind(ty).instantiate(tcx, new_args);
let old_ty =
ty::EarlyBinder::bind(ty).instantiate(tcx, generic_args).skip_normalization();
let new_ty =
ty::EarlyBinder::bind(ty).instantiate(tcx, new_args).skip_normalization();
if let Ok(old_ty) = tcx.try_normalize_erasing_regions(
Copy link
Copy Markdown
Contributor

@lcnr lcnr Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

I would try to change fn try_normalize_erasing_regions to take Unnormalized<T> and return T and then avoid the skip_normalization here

self.infcx.typing_env(self.infcx.param_env),
old_ty,
Expand All @@ -754,21 +761,23 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}

// Test the callee's predicates, substituting in `ref_ty` for the moved argument type.
clauses.instantiate(tcx, new_args).predicates.iter().all(|&(mut clause)| {
// Normalize before testing to see through type aliases and projections.
if let Ok(normalized) = tcx.try_normalize_erasing_regions(
self.infcx.typing_env(self.infcx.param_env),
clause,
) {
clause = normalized;
}
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
tcx,
ObligationCause::dummy(),
self.infcx.param_env,
clause,
))
})
clauses.instantiate(tcx, new_args).skip_normalization().predicates.iter().all(
|&(mut clause)| {
// Normalize before testing to see through type aliases and projections.
if let Ok(normalized) = tcx.try_normalize_erasing_regions(
self.infcx.typing_env(self.infcx.param_env),
clause,
) {
clause = normalized;
}
self.infcx.predicate_must_hold_modulo_regions(&Obligation::new(
tcx,
ObligationCause::dummy(),
self.infcx.param_env,
clause,
))
},
)
}) {
let place_desc = if let Some(desc) = self.describe_place(moved_place) {
format!("`{desc}`")
Expand Down Expand Up @@ -4153,11 +4162,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
if is_closure {
None
} else {
let ty = self.infcx.tcx.type_of(self.mir_def_id()).instantiate_identity();
let ty = self
.infcx
.tcx
.type_of(self.mir_def_id())
.instantiate_identity()
.skip_normalization();
match ty.kind() {
ty::FnDef(_, _) | ty::FnPtr(..) => self.annotate_fn_sig(
self.mir_def_id(),
self.infcx.tcx.fn_sig(self.mir_def_id()).instantiate_identity(),
self.infcx
.tcx
.fn_sig(self.mir_def_id())
.instantiate_identity()
.skip_normalization(),
),
_ => None,
}
Expand Down
18 changes: 14 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let parent_self_ty =
matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
.then_some(parent_did)
.and_then(|did| match tcx.type_of(did).instantiate_identity().kind() {
ty::Adt(def, ..) => Some(def.did()),
_ => None,
.and_then(|did| {
match tcx
.type_of(did)
.instantiate_identity()
.skip_normalization()
.kind()
{
ty::Adt(def, ..) => Some(def.did()),
_ => None,
}
});
let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
Expand Down Expand Up @@ -1445,7 +1452,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
&& let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
fn_call_span,
BoundRegionConversionTime::FnCall,
tcx.fn_sig(method_did).instantiate(tcx, method_args).input(0),
tcx.fn_sig(method_did)
.instantiate(tcx, method_args)
.skip_normalization()
.input(0),
)
&& self.infcx.can_eq(self.infcx.param_env, ty, self_ty)
{
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
hir::ExprKind::Call(callee, _) => {
let ty = typeck_result.node_type_opt(callee.hir_id)?;
let ty::FnDef(fn_def_id, args) = *ty.kind() else { return None };
tcx.predicates_of(fn_def_id).instantiate(tcx, args)
tcx.predicates_of(fn_def_id).instantiate(tcx, args).skip_normalization()
}
hir::ExprKind::MethodCall(..) => {
let (_, method) = typeck_result.type_dependent_def(parent.hir_id)?;
let args = typeck_result.node_args(parent.hir_id);
tcx.predicates_of(method).instantiate(tcx, args)
tcx.predicates_of(method).instantiate(tcx, args).skip_normalization()
}
_ => return None,
};
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_hir::def_id::DefId;
use rustc_middle::mir::{self, ConstraintCategory, Location};
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
Unnormalized,
};
use rustc_span::Span;
use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_region_diagnostic;
Expand Down Expand Up @@ -282,6 +283,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for CheckExplicitRegionMentionAndCollectGen
.tcx
.explicit_item_bounds(def_id)
.iter_instantiated_copied(self.tcx, opaque.args)
.map(Unnormalized::skip_normalization)
{
bound.visit_with(self)?;
}
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity().skip_normalization()
};

debug!("report_fnmut_error: output_ty={:?}", output_ty);
Expand Down Expand Up @@ -930,7 +930,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
debug!(?fn_did, ?args);

// Only suggest this on function calls, not closures
let ty = tcx.type_of(fn_did).instantiate_identity();
let ty = tcx.type_of(fn_did).instantiate_identity().skip_normalization();
debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
if let ty::Closure(_, _) = ty.kind() {
return;
Expand Down Expand Up @@ -1050,7 +1050,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
else {
return;
};
let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
let ty::Closure(_, args) =
*tcx.type_of(closure_def_id).instantiate_identity().skip_normalization().kind()
else {
return;
};
Expand Down Expand Up @@ -1143,7 +1144,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
});

let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args).skip_normalization();

let ocx = ObligationCtxt::new(&self.infcx);
ocx.register_obligations(preds.iter().map(|(pred, span)| {
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {

// Get the parent fn's signature with liberated late-bound regions,
// so we have `ReLateParam` instead of `ReBound`.
let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity();
let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity().skip_normalization();
let liberated_sig = tcx.liberate_late_bound_regions(parent_def_id, parent_fn_sig);
let parent_param_ty = *liberated_sig.inputs().get(param_index)?;

Expand Down Expand Up @@ -1023,10 +1023,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
return None;
};

let found = tcx
.any_free_region_meets(&tcx.type_of(region_parent).instantiate_identity(), |r| {
r.kind() == ty::ReEarlyParam(region)
});
let found = tcx.any_free_region_meets(
&tcx.type_of(region_parent).instantiate_identity().skip_normalization(),
|r| r.kind() == ty::ReEarlyParam(region),
);

Some(RegionName {
name: self.synthesize_region_name(),
Expand Down Expand Up @@ -1056,6 +1056,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
.tcx
.predicates_of(self.body.source.def_id())
.instantiate_identity(self.infcx.tcx)
.skip_normalization()
.predicates;

if let Some(upvar_index) = self
Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,17 @@ pub(crate) fn apply_definition_site_hidden_types<'tcx>(
};

// We erase all non-member region of the opaque and need to treat these as existentials.
let expected_ty =
ty::fold_regions(tcx, expected.ty.instantiate(tcx, key.args), |re, _dbi| {
match re.kind() {
ty::ReErased => infcx.next_nll_region_var(
NllRegionVariableOrigin::Existential { name: None },
|| crate::RegionCtxt::Existential(None),
),
_ => re,
}
});
let expected_ty = ty::fold_regions(
tcx,
expected.ty.instantiate(tcx, key.args).skip_normalization(),
|re, _dbi| match re.kind() {
ty::ReErased => infcx.next_nll_region_var(
NllRegionVariableOrigin::Existential { name: None },
|| crate::RegionCtxt::Existential(None),
),
_ => re,
},
);

// We now simply equate the expected with the actual hidden type.
let locations = Locations::All(hidden_type.span);
Expand Down
Loading
Loading