Skip to content

Commit eb76764

Browse files
committed
Auto merge of #113120 - Dylan-DPC:rollup-cz4qr3o, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #111571 (Implement proposed API for `proc_macro_span`) - #112236 (Simplify computation of killed borrows) - #112867 (More `ImplSource` nits) - #113019 (add note for non-exhaustive matches with guards) - #113094 (Fix invalid HTML DIV tag used in HEAD) - #113111 (add myself to review for t-types stuff) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8882507 + 11c8dbf commit eb76764

File tree

41 files changed

+230
-309
lines changed

Some content is hidden

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

41 files changed

+230
-309
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -2580,9 +2580,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
25802580

25812581
[[package]]
25822582
name = "proc-macro2"
2583-
version = "1.0.56"
2583+
version = "1.0.60"
25842584
source = "registry+https://github.com/rust-lang/crates.io-index"
2585-
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
2585+
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
25862586
dependencies = [
25872587
"unicode-ident",
25882588
]

compiler/rustc_borrowck/src/dataflow.rs

+43-51
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,9 @@ pub struct Borrows<'a, 'tcx> {
125125
borrows_out_of_scope_at_location: FxIndexMap<Location, Vec<BorrowIndex>>,
126126
}
127127

128-
struct StackEntry {
129-
bb: mir::BasicBlock,
130-
lo: usize,
131-
hi: usize,
132-
}
133-
134128
struct OutOfScopePrecomputer<'a, 'tcx> {
135129
visited: BitSet<mir::BasicBlock>,
136-
visit_stack: Vec<StackEntry>,
130+
visit_stack: Vec<mir::BasicBlock>,
137131
body: &'a Body<'tcx>,
138132
regioncx: &'a RegionInferenceContext<'tcx>,
139133
borrows_out_of_scope_at_location: FxIndexMap<Location, Vec<BorrowIndex>>,
@@ -158,68 +152,66 @@ impl<'tcx> OutOfScopePrecomputer<'_, 'tcx> {
158152
borrow_region: RegionVid,
159153
first_location: Location,
160154
) {
161-
// We visit one BB at a time. The complication is that we may start in the
162-
// middle of the first BB visited (the one containing `first_location`), in which
163-
// case we may have to later on process the first part of that BB if there
164-
// is a path back to its start.
165-
166-
// For visited BBs, we record the index of the first statement processed.
167-
// (In fully processed BBs this index is 0.) Note also that we add BBs to
168-
// `visited` once they are added to `stack`, before they are actually
169-
// processed, because this avoids the need to look them up again on
170-
// completion.
171-
self.visited.insert(first_location.block);
172-
173155
let first_block = first_location.block;
174-
let mut first_lo = first_location.statement_index;
175-
let first_hi = self.body[first_block].statements.len();
156+
let first_bb_data = &self.body.basic_blocks[first_block];
157+
158+
// This is the first block, we only want to visit it from the creation of the borrow at
159+
// `first_location`.
160+
let first_lo = first_location.statement_index;
161+
let first_hi = first_bb_data.statements.len();
162+
163+
if let Some(kill_stmt) = self.regioncx.first_non_contained_inclusive(
164+
borrow_region,
165+
first_block,
166+
first_lo,
167+
first_hi,
168+
) {
169+
let kill_location = Location { block: first_block, statement_index: kill_stmt };
170+
// If region does not contain a point at the location, then add to list and skip
171+
// successor locations.
172+
debug!("borrow {:?} gets killed at {:?}", borrow_index, kill_location);
173+
self.borrows_out_of_scope_at_location
174+
.entry(kill_location)
175+
.or_default()
176+
.push(borrow_index);
177+
178+
// The borrow is already dead, there is no need to visit other blocks.
179+
return;
180+
}
176181

177-
self.visit_stack.push(StackEntry { bb: first_block, lo: first_lo, hi: first_hi });
182+
// The borrow is not dead. Add successor BBs to the work list, if necessary.
183+
for succ_bb in first_bb_data.terminator().successors() {
184+
if self.visited.insert(succ_bb) {
185+
self.visit_stack.push(succ_bb);
186+
}
187+
}
178188

179-
'preorder: while let Some(StackEntry { bb, lo, hi }) = self.visit_stack.pop() {
189+
// We may end up visiting `first_block` again. This is not an issue: we know at this point
190+
// that it does not kill the borrow in the `first_lo..=first_hi` range, so checking the
191+
// `0..first_lo` range and the `0..first_hi` range give the same result.
192+
while let Some(block) = self.visit_stack.pop() {
193+
let bb_data = &self.body[block];
194+
let num_stmts = bb_data.statements.len();
180195
if let Some(kill_stmt) =
181-
self.regioncx.first_non_contained_inclusive(borrow_region, bb, lo, hi)
196+
self.regioncx.first_non_contained_inclusive(borrow_region, block, 0, num_stmts)
182197
{
183-
let kill_location = Location { block: bb, statement_index: kill_stmt };
198+
let kill_location = Location { block, statement_index: kill_stmt };
184199
// If region does not contain a point at the location, then add to list and skip
185200
// successor locations.
186201
debug!("borrow {:?} gets killed at {:?}", borrow_index, kill_location);
187202
self.borrows_out_of_scope_at_location
188203
.entry(kill_location)
189204
.or_default()
190205
.push(borrow_index);
191-
continue 'preorder;
192-
}
193206

194-
// If we process the first part of the first basic block (i.e. we encounter that block
195-
// for the second time), we no longer have to visit its successors again.
196-
if bb == first_block && hi != first_hi {
207+
// We killed the borrow, so we do not visit this block's successors.
197208
continue;
198209
}
199210

200211
// Add successor BBs to the work list, if necessary.
201-
let bb_data = &self.body[bb];
202-
debug_assert!(hi == bb_data.statements.len());
203212
for succ_bb in bb_data.terminator().successors() {
204-
if !self.visited.insert(succ_bb) {
205-
if succ_bb == first_block && first_lo > 0 {
206-
// `succ_bb` has been seen before. If it wasn't
207-
// fully processed, add its first part to `stack`
208-
// for processing.
209-
self.visit_stack.push(StackEntry { bb: succ_bb, lo: 0, hi: first_lo - 1 });
210-
211-
// And update this entry with 0, to represent the
212-
// whole BB being processed.
213-
first_lo = 0;
214-
}
215-
} else {
216-
// succ_bb hasn't been seen before. Add it to
217-
// `stack` for processing.
218-
self.visit_stack.push(StackEntry {
219-
bb: succ_bb,
220-
lo: 0,
221-
hi: self.body[succ_bb].statements.len(),
222-
});
213+
if self.visited.insert(succ_bb) {
214+
self.visit_stack.push(succ_bb);
223215
}
224216
}
225217
}

compiler/rustc_expand/src/proc_macro_server.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::base::ExtCtxt;
22
use pm::bridge::{
33
server, DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree,
44
};
5-
use pm::{Delimiter, Level, LineColumn};
5+
use pm::{Delimiter, Level};
66
use rustc_ast as ast;
77
use rustc_ast::token;
88
use rustc_ast::tokenstream::{self, Spacing::*, TokenStream};
@@ -648,23 +648,22 @@ impl server::Span for Rustc<'_, '_> {
648648

649649
Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
650650
}
651-
652-
fn start(&mut self, span: Self::Span) -> LineColumn {
653-
let loc = self.sess().source_map().lookup_char_pos(span.lo());
654-
LineColumn { line: loc.line, column: loc.col.to_usize() }
651+
fn start(&mut self, span: Self::Span) -> Self::Span {
652+
span.shrink_to_lo()
655653
}
656654

657-
fn end(&mut self, span: Self::Span) -> LineColumn {
658-
let loc = self.sess().source_map().lookup_char_pos(span.hi());
659-
LineColumn { line: loc.line, column: loc.col.to_usize() }
655+
fn end(&mut self, span: Self::Span) -> Self::Span {
656+
span.shrink_to_hi()
660657
}
661658

662-
fn before(&mut self, span: Self::Span) -> Self::Span {
663-
span.shrink_to_lo()
659+
fn line(&mut self, span: Self::Span) -> usize {
660+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
661+
loc.line
664662
}
665663

666-
fn after(&mut self, span: Self::Span) -> Self::Span {
667-
span.shrink_to_hi()
664+
fn column(&mut self, span: Self::Span) -> usize {
665+
let loc = self.sess().source_map().lookup_char_pos(span.lo());
666+
loc.col.to_usize() + 1
668667
}
669668

670669
fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {

compiler/rustc_middle/src/traits/mod.rs

-23
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,6 @@ pub enum ImplSource<'tcx, N> {
660660

661661
/// ImplSource for trait upcasting coercion
662662
TraitUpcasting(ImplSourceTraitUpcastingData<N>),
663-
664-
/// ImplSource for a trait alias.
665-
TraitAlias(ImplSourceTraitAliasData<'tcx, N>),
666663
}
667664

668665
impl<'tcx, N> ImplSource<'tcx, N> {
@@ -671,7 +668,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
671668
ImplSource::UserDefined(i) => i.nested,
672669
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
673670
ImplSource::Object(d) => d.nested,
674-
ImplSource::TraitAlias(d) => d.nested,
675671
ImplSource::TraitUpcasting(d) => d.nested,
676672
}
677673
}
@@ -681,7 +677,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
681677
ImplSource::UserDefined(i) => &i.nested,
682678
ImplSource::Param(n, _) | ImplSource::Builtin(n) => &n,
683679
ImplSource::Object(d) => &d.nested,
684-
ImplSource::TraitAlias(d) => &d.nested,
685680
ImplSource::TraitUpcasting(d) => &d.nested,
686681
}
687682
}
@@ -691,7 +686,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
691686
ImplSource::UserDefined(i) => &mut i.nested,
692687
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
693688
ImplSource::Object(d) => &mut d.nested,
694-
ImplSource::TraitAlias(d) => &mut d.nested,
695689
ImplSource::TraitUpcasting(d) => &mut d.nested,
696690
}
697691
}
@@ -709,15 +703,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
709703
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
710704
ImplSource::Builtin(n) => ImplSource::Builtin(n.into_iter().map(f).collect()),
711705
ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData {
712-
upcast_trait_def_id: o.upcast_trait_def_id,
713706
vtable_base: o.vtable_base,
714707
nested: o.nested.into_iter().map(f).collect(),
715708
}),
716-
ImplSource::TraitAlias(d) => ImplSource::TraitAlias(ImplSourceTraitAliasData {
717-
alias_def_id: d.alias_def_id,
718-
substs: d.substs,
719-
nested: d.nested.into_iter().map(f).collect(),
720-
}),
721709
ImplSource::TraitUpcasting(d) => {
722710
ImplSource::TraitUpcasting(ImplSourceTraitUpcastingData {
723711
vtable_vptr_slot: d.vtable_vptr_slot,
@@ -761,9 +749,6 @@ pub struct ImplSourceTraitUpcastingData<N> {
761749
#[derive(PartialEq, Eq, Clone, TyEncodable, TyDecodable, HashStable, Lift)]
762750
#[derive(TypeFoldable, TypeVisitable)]
763751
pub struct ImplSourceObjectData<N> {
764-
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
765-
pub upcast_trait_def_id: DefId,
766-
767752
/// The vtable is formed by concatenating together the method lists of
768753
/// the base object trait and all supertraits, pointers to supertrait vtable will
769754
/// be provided when necessary; this is the start of `upcast_trait_ref`'s methods
@@ -773,14 +758,6 @@ pub struct ImplSourceObjectData<N> {
773758
pub nested: Vec<N>,
774759
}
775760

776-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
777-
#[derive(TypeFoldable, TypeVisitable)]
778-
pub struct ImplSourceTraitAliasData<'tcx, N> {
779-
pub alias_def_id: DefId,
780-
pub substs: SubstsRef<'tcx>,
781-
pub nested: Vec<N>,
782-
}
783-
784761
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
785762
pub enum ObjectSafetyViolation {
786763
/// `Self: Sized` declared on the trait.

compiler/rustc_middle/src/traits/structural_impls.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
1717
write!(f, "ImplSourceParamData({:?}, {:?})", n, ct)
1818
}
1919

20-
super::ImplSource::TraitAlias(ref d) => write!(f, "{:?}", d),
21-
2220
super::ImplSource::TraitUpcasting(ref d) => write!(f, "{:?}", d),
2321
}
2422
}
@@ -48,18 +46,8 @@ impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<N> {
4846
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4947
write!(
5048
f,
51-
"ImplSourceObjectData(upcast={:?}, vtable_base={}, nested={:?})",
52-
self.upcast_trait_def_id, self.vtable_base, self.nested
53-
)
54-
}
55-
}
56-
57-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx, N> {
58-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59-
write!(
60-
f,
61-
"ImplSourceTraitAliasData(alias_def_id={:?}, substs={:?}, nested={:?})",
62-
self.alias_def_id, self.substs, self.nested
49+
"ImplSourceObjectData(vtable_base={}, nested={:?})",
50+
self.vtable_base, self.nested
6351
)
6452
}
6553
}

compiler/rustc_mir_build/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ mir_build_mutation_of_layout_constrained_field_requires_unsafe_unsafe_op_in_unsa
214214
215215
mir_build_non_const_path = runtime values cannot be referenced in patterns
216216
217+
mir_build_non_exhaustive_match_all_arms_guarded =
218+
match arms with guards don't count towards exhaustivity
219+
217220
mir_build_non_exhaustive_omitted_pattern = some variants are not matched explicitly
218221
.help = ensure that all variants are matched explicitly by adding the suggested match arms
219222
.note = the matched value is of type `{$scrut_ty}` and the `non_exhaustive_omitted_patterns` attribute was found

compiler/rustc_mir_build/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ impl<'a> IntoDiagnostic<'a> for NonExhaustivePatternsTypeNotEmpty<'_, '_, '_> {
432432
}
433433
}
434434

435+
#[derive(Subdiagnostic)]
436+
#[note(mir_build_non_exhaustive_match_all_arms_guarded)]
437+
pub struct NonExhaustiveMatchAllArmsGuarded;
438+
435439
#[derive(Diagnostic)]
436440
#[diag(mir_build_static_in_pattern, code = "E0158")]
437441
pub struct StaticInPattern {

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+5
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,11 @@ fn non_exhaustive_match<'p, 'tcx>(
830830
_ => " or multiple match arms",
831831
},
832832
);
833+
834+
let all_arms_have_guards = arms.iter().all(|arm_id| thir[*arm_id].guard.is_some());
835+
if !is_empty_match && all_arms_have_guards {
836+
err.subdiagnostic(NonExhaustiveMatchAllArmsGuarded);
837+
}
833838
if let Some((span, sugg)) = suggestion {
834839
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
835840
} else {

0 commit comments

Comments
 (0)