Skip to content

Commit 4084b9d

Browse files
committed
Auto merge of rust-lang#123473 - matthiaskrgr:rollup-pqnczcg, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#123311 (Match ergonomics: implement "`&`pat everywhere") - rust-lang#123317 (Support running library tests in Miri) - rust-lang#123363 (change `NormalizesTo` to fully structurally normalize) - rust-lang#123417 (Add Description for cargo in rustdoc documentation) - rust-lang#123454 (hir: Use `ItemLocalId::ZERO` in a couple more places) - rust-lang#123464 (Cleanup: Rename `HAS_PROJECTIONS` to `HAS_ALIASES` etc.) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 385fa9d + 7dcc1da commit 4084b9d

File tree

58 files changed

+948
-414
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+948
-414
lines changed

compiler/rustc_ast_lowering/src/index.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct NodeCollector<'a, 'hir> {
1919
parenting: LocalDefIdMap<ItemLocalId>,
2020

2121
/// The parent of this node
22-
parent_node: hir::ItemLocalId,
22+
parent_node: ItemLocalId,
2323

2424
owner: OwnerId,
2525
}
@@ -31,17 +31,16 @@ pub(super) fn index_hir<'hir>(
3131
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
3232
num_nodes: usize,
3333
) -> (IndexVec<ItemLocalId, ParentedNode<'hir>>, LocalDefIdMap<ItemLocalId>) {
34-
let zero_id = ItemLocalId::ZERO;
35-
let err_node = ParentedNode { parent: zero_id, node: Node::Err(item.span()) };
34+
let err_node = ParentedNode { parent: ItemLocalId::ZERO, node: Node::Err(item.span()) };
3635
let mut nodes = IndexVec::from_elem_n(err_node, num_nodes);
3736
// This node's parent should never be accessed: the owner's parent is computed by the
3837
// hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is
3938
// used.
40-
nodes[zero_id] = ParentedNode { parent: ItemLocalId::INVALID, node: item.into() };
39+
nodes[ItemLocalId::ZERO] = ParentedNode { parent: ItemLocalId::INVALID, node: item.into() };
4140
let mut collector = NodeCollector {
4241
tcx,
4342
owner: item.def_id(),
44-
parent_node: zero_id,
43+
parent_node: ItemLocalId::ZERO,
4544
nodes,
4645
bodies,
4746
parenting: Default::default(),
@@ -112,7 +111,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
112111
}
113112

114113
fn insert_nested(&mut self, item: LocalDefId) {
115-
if self.parent_node.as_u32() != 0 {
114+
if self.parent_node != ItemLocalId::ZERO {
116115
self.parenting.insert(item, self.parent_node);
117116
}
118117
}

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ declare_features! (
567567
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
568568
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.
569569
(unstable, raw_ref_op, "1.41.0", Some(64490)),
570+
/// Allows `&` and `&mut` patterns to consume match-ergonomics-inserted references.
571+
(incomplete, ref_pat_everywhere, "CURRENT_RUSTC_VERSION", Some(123076)),
570572
/// Allows using the `#[register_tool]` attribute.
571573
(unstable, register_tool, "1.41.0", Some(66079)),
572574
/// Allows the `#[repr(i128)]` attribute for enums.

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
461461
where
462462
T: TypeVisitable<TyCtxt<'tcx>>,
463463
{
464-
t.has_free_regions() || t.has_projections() || t.has_infer_types()
464+
t.has_free_regions() || t.has_aliases() || t.has_infer_types()
465465
}
466466

467467
pub fn node_ty(&self, id: hir::HirId) -> Ty<'tcx> {

compiler/rustc_hir_typeck/src/pat.rs

+64-27
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ enum AdjustMode {
130130
/// Peel off all immediate reference types.
131131
Peel,
132132
/// Reset binding mode to the initial mode.
133+
/// Used for destructuring assignment, where we don't want any match ergonomics.
133134
Reset,
135+
/// Produced by ref patterns.
136+
/// Reset the binding mode to the initial mode,
137+
/// and if the old biding mode was by-reference
138+
/// with mutability matching the pattern,
139+
/// mark the pattern as having consumed this reference.
140+
ResetAndConsumeRef(Mutability),
134141
/// Pass on the input binding mode and expected type.
135142
Pass,
136143
}
@@ -174,7 +181,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174181
_ => None,
175182
};
176183
let adjust_mode = self.calc_adjust_mode(pat, path_res.map(|(res, ..)| res));
177-
let (expected, def_bm) = self.calc_default_binding_mode(pat, expected, def_bm, adjust_mode);
184+
let (expected, def_bm, ref_pattern_already_consumed) =
185+
self.calc_default_binding_mode(pat, expected, def_bm, adjust_mode);
178186
let pat_info = PatInfo {
179187
binding_mode: def_bm,
180188
top_info: ti,
@@ -211,7 +219,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211219
}
212220
PatKind::Box(inner) => self.check_pat_box(pat.span, inner, expected, pat_info),
213221
PatKind::Deref(inner) => self.check_pat_deref(pat.span, inner, expected, pat_info),
214-
PatKind::Ref(inner, mutbl) => self.check_pat_ref(pat, inner, mutbl, expected, pat_info),
222+
PatKind::Ref(inner, mutbl) => self.check_pat_ref(
223+
pat,
224+
inner,
225+
mutbl,
226+
expected,
227+
pat_info,
228+
ref_pattern_already_consumed,
229+
),
215230
PatKind::Slice(before, slice, after) => {
216231
self.check_pat_slice(pat.span, before, slice, after, expected, pat_info)
217232
}
@@ -264,17 +279,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264279

265280
/// Compute the new expected type and default binding mode from the old ones
266281
/// as well as the pattern form we are currently checking.
282+
///
283+
/// Last entry is only relevant for ref patterns (`&` and `&mut`);
284+
/// if `true`, then the ref pattern consumed a match ergonomics inserted reference
285+
/// and so does no need to match against a reference in the scrutinee type.
267286
fn calc_default_binding_mode(
268287
&self,
269288
pat: &'tcx Pat<'tcx>,
270289
expected: Ty<'tcx>,
271290
def_bm: BindingAnnotation,
272291
adjust_mode: AdjustMode,
273-
) -> (Ty<'tcx>, BindingAnnotation) {
292+
) -> (Ty<'tcx>, BindingAnnotation, bool) {
274293
match adjust_mode {
275-
AdjustMode::Pass => (expected, def_bm),
276-
AdjustMode::Reset => (expected, INITIAL_BM),
277-
AdjustMode::Peel => self.peel_off_references(pat, expected, def_bm),
294+
AdjustMode::Pass => (expected, def_bm, false),
295+
AdjustMode::Reset => (expected, INITIAL_BM, false),
296+
AdjustMode::ResetAndConsumeRef(mutbl) => {
297+
(expected, INITIAL_BM, def_bm.0 == ByRef::Yes(mutbl))
298+
}
299+
AdjustMode::Peel => {
300+
let peeled = self.peel_off_references(pat, expected, def_bm);
301+
(peeled.0, peeled.1, false)
302+
}
278303
}
279304
}
280305

@@ -329,7 +354,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
329354
// ```
330355
//
331356
// See issue #46688.
332-
PatKind::Ref(..) => AdjustMode::Reset,
357+
PatKind::Ref(_, mutbl) => AdjustMode::ResetAndConsumeRef(*mutbl),
333358
// A `_` pattern works with any expected type, so there's no need to do anything.
334359
PatKind::Wild
335360
// A malformed pattern doesn't have an expected type, so let's just accept any type.
@@ -840,8 +865,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
840865
&& let Some(mt) = self.shallow_resolve(expected).builtin_deref(true)
841866
&& let ty::Dynamic(..) = mt.ty.kind()
842867
{
843-
// This is "x = SomeTrait" being reduced from
844-
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
868+
// This is "x = dyn SomeTrait" being reduced from
869+
// "let &x = &dyn SomeTrait" or "let box x = Box<dyn SomeTrait>", an error.
845870
let type_str = self.ty_to_string(expected);
846871
let mut err = struct_span_code_err!(
847872
self.dcx(),
@@ -2036,6 +2061,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20362061
mutbl: Mutability,
20372062
expected: Ty<'tcx>,
20382063
pat_info: PatInfo<'tcx, '_>,
2064+
consumed_inherited_ref: bool,
20392065
) -> Ty<'tcx> {
20402066
let tcx = self.tcx;
20412067
let expected = self.shallow_resolve(expected);
@@ -2051,26 +2077,37 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20512077
match *expected.kind() {
20522078
ty::Ref(_, r_ty, r_mutbl) if r_mutbl == mutbl => (expected, r_ty),
20532079
_ => {
2054-
let inner_ty = self.next_ty_var(TypeVariableOrigin {
2055-
kind: TypeVariableOriginKind::TypeInference,
2056-
span: inner.span,
2057-
});
2058-
let ref_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
2059-
debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2060-
let err = self.demand_eqtype_pat_diag(
2061-
pat.span,
2062-
expected,
2063-
ref_ty,
2064-
pat_info.top_info,
2065-
);
2080+
if consumed_inherited_ref && self.tcx.features().ref_pat_everywhere {
2081+
// We already matched against a match-ergonmics inserted reference,
2082+
// so we don't need to match against a reference from the original type.
2083+
// Save this infor for use in lowering later
2084+
self.typeck_results
2085+
.borrow_mut()
2086+
.skipped_ref_pats_mut()
2087+
.insert(pat.hir_id);
2088+
(expected, expected)
2089+
} else {
2090+
let inner_ty = self.next_ty_var(TypeVariableOrigin {
2091+
kind: TypeVariableOriginKind::TypeInference,
2092+
span: inner.span,
2093+
});
2094+
let ref_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
2095+
debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2096+
let err = self.demand_eqtype_pat_diag(
2097+
pat.span,
2098+
expected,
2099+
ref_ty,
2100+
pat_info.top_info,
2101+
);
20662102

2067-
// Look for a case like `fn foo(&foo: u32)` and suggest
2068-
// `fn foo(foo: &u32)`
2069-
if let Some(mut err) = err {
2070-
self.borrow_pat_suggestion(&mut err, pat);
2071-
err.emit();
2103+
// Look for a case like `fn foo(&foo: u32)` and suggest
2104+
// `fn foo(foo: &u32)`
2105+
if let Some(mut err) = err {
2106+
self.borrow_pat_suggestion(&mut err, pat);
2107+
err.emit();
2108+
}
2109+
(ref_ty, inner_ty)
20722110
}
2073-
(ref_ty, inner_ty)
20742111
}
20752112
}
20762113
}

compiler/rustc_hir_typeck/src/writeback.rs

+9
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
345345
_ => {}
346346
};
347347

348+
self.visit_skipped_ref_pats(p.hir_id);
348349
self.visit_pat_adjustments(p.span, p.hir_id);
349350

350351
self.visit_node_id(p.span, p.hir_id);
@@ -674,6 +675,14 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
674675
}
675676
}
676677

678+
#[instrument(skip(self), level = "debug")]
679+
fn visit_skipped_ref_pats(&mut self, hir_id: hir::HirId) {
680+
if self.fcx.typeck_results.borrow_mut().skipped_ref_pats_mut().remove(hir_id) {
681+
debug!("node is a skipped ref pat");
682+
self.typeck_results.skipped_ref_pats_mut().insert(hir_id);
683+
}
684+
}
685+
677686
fn visit_liberated_fn_sigs(&mut self) {
678687
let fcx_typeck_results = self.fcx.typeck_results.borrow();
679688
assert_eq!(fcx_typeck_results.hir_owner, self.typeck_results.hir_owner);

compiler/rustc_middle/src/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub fn provide(providers: &mut Providers) {
179179
.parenting
180180
.get(&owner_id.def_id)
181181
.copied()
182-
.unwrap_or(ItemLocalId::from_u32(0)),
182+
.unwrap_or(ItemLocalId::ZERO),
183183
}
184184
})
185185
};

compiler/rustc_middle/src/traits/solve/inspect.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ pub enum ProbeStep<'tcx> {
121121
/// used whenever there are multiple candidates to prove the
122122
/// current goalby .
123123
NestedProbe(Probe<'tcx>),
124-
CommitIfOkStart,
125-
CommitIfOkSuccess,
126124
}
127125

128126
/// What kind of probe we're in. In case the probe represents a candidate, or
@@ -132,6 +130,8 @@ pub enum ProbeStep<'tcx> {
132130
pub enum ProbeKind<'tcx> {
133131
/// The root inference context while proving a goal.
134132
Root { result: QueryResult<'tcx> },
133+
/// Trying to normalize an alias by at least one stpe in `NormalizesTo`.
134+
TryNormalizeNonRigid { result: QueryResult<'tcx> },
135135
/// Probe entered when normalizing the self ty during candidate assembly
136136
NormalizedSelfTyAssembly,
137137
/// Some candidate to prove the current goal.
@@ -143,9 +143,6 @@ pub enum ProbeKind<'tcx> {
143143
/// Used in the probe that wraps normalizing the non-self type for the unsize
144144
/// trait, which is also structurally matched on.
145145
UnsizeAssembly,
146-
/// A call to `EvalCtxt::commit_if_ok` which failed, causing the work
147-
/// to be discarded.
148-
CommitIfOk,
149146
/// During upcasting from some source object to target object type, used to
150147
/// do a probe to find out what projection type(s) may be used to prove that
151148
/// the source type upholds all of the target type's object bounds.

compiler/rustc_middle/src/traits/solve/inspect/format.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
100100
ProbeKind::Root { result } => {
101101
write!(self.f, "ROOT RESULT: {result:?}")
102102
}
103+
ProbeKind::TryNormalizeNonRigid { result } => {
104+
write!(self.f, "TRY NORMALIZE NON-RIGID: {result:?}")
105+
}
103106
ProbeKind::NormalizedSelfTyAssembly => {
104107
write!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:")
105108
}
@@ -109,9 +112,6 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
109112
ProbeKind::UpcastProjectionCompatibility => {
110113
write!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:")
111114
}
112-
ProbeKind::CommitIfOk => {
113-
write!(self.f, "COMMIT_IF_OK:")
114-
}
115115
ProbeKind::MiscCandidate { name, result } => {
116116
write!(self.f, "CANDIDATE {name}: {result:?}")
117117
}
@@ -132,8 +132,6 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
132132
}
133133
ProbeStep::EvaluateGoals(eval) => this.format_added_goals_evaluation(eval)?,
134134
ProbeStep::NestedProbe(probe) => this.format_probe(probe)?,
135-
ProbeStep::CommitIfOkStart => writeln!(this.f, "COMMIT_IF_OK START")?,
136-
ProbeStep::CommitIfOkSuccess => writeln!(this.f, "COMMIT_IF_OK SUCCESS")?,
137135
}
138136
}
139137
Ok(())

compiler/rustc_middle/src/ty/normalize_erasing_regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'tcx> TyCtxt<'tcx> {
4949
let value = self.erase_regions(value);
5050
debug!(?value);
5151

52-
if !value.has_projections() {
52+
if !value.has_aliases() {
5353
value
5454
} else {
5555
value.fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env })
@@ -81,7 +81,7 @@ impl<'tcx> TyCtxt<'tcx> {
8181
let value = self.erase_regions(value);
8282
debug!(?value);
8383

84-
if !value.has_projections() {
84+
if !value.has_aliases() {
8585
Ok(value)
8686
} else {
8787
let mut folder = TryNormalizeAfterErasingRegionsFolder::new(self, param_env);

0 commit comments

Comments
 (0)