Skip to content

Commit 336efc6

Browse files
committed
Auto merge of #151192 - matthiaskrgr:rollup-uThdZGN, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #151155 (THIR patterns: Always use type `str` for string-constant-value nodes) - #151166 (fix: Do not delay E0107 when there exists an assoc ty with the same name) - #151172 (Use default field values in a few more cases) - #151185 (Disable `dump-ice-to-disk` on `i686-pc-windows-msvc`) r? @ghost
2 parents bcf787a + 36d4a6d commit 336efc6

16 files changed

Lines changed: 524 additions & 174 deletions

File tree

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,12 +2312,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
23122312
tcx: TyCtxt<'hir>,
23132313
issue_span: Span,
23142314
expr_span: Span,
2315-
body_expr: Option<&'hir hir::Expr<'hir>>,
2316-
loop_bind: Option<&'hir Ident>,
2317-
loop_span: Option<Span>,
2318-
head_span: Option<Span>,
2319-
pat_span: Option<Span>,
2320-
head: Option<&'hir hir::Expr<'hir>>,
2315+
body_expr: Option<&'hir hir::Expr<'hir>> = None,
2316+
loop_bind: Option<&'hir Ident> = None,
2317+
loop_span: Option<Span> = None,
2318+
head_span: Option<Span> = None,
2319+
pat_span: Option<Span> = None,
2320+
head: Option<&'hir hir::Expr<'hir>> = None,
23212321
}
23222322
impl<'hir> Visitor<'hir> for ExprFinder<'hir> {
23232323
fn visit_expr(&mut self, ex: &'hir hir::Expr<'hir>) {
@@ -2383,17 +2383,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
23832383
hir::intravisit::walk_expr(self, ex);
23842384
}
23852385
}
2386-
let mut finder = ExprFinder {
2387-
tcx,
2388-
expr_span: span,
2389-
issue_span,
2390-
loop_bind: None,
2391-
body_expr: None,
2392-
head_span: None,
2393-
loop_span: None,
2394-
pat_span: None,
2395-
head: None,
2396-
};
2386+
let mut finder = ExprFinder { tcx, expr_span: span, issue_span, .. };
23972387
finder.visit_expr(tcx.hir_body(body_id).value);
23982388

23992389
if let Some(body_expr) = finder.body_expr
@@ -2628,13 +2618,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
26282618

26292619
struct ExpressionFinder<'tcx> {
26302620
capture_span: Span,
2631-
closure_change_spans: Vec<Span>,
2632-
closure_arg_span: Option<Span>,
2633-
in_closure: bool,
2634-
suggest_arg: String,
2621+
closure_change_spans: Vec<Span> = vec![],
2622+
closure_arg_span: Option<Span> = None,
2623+
in_closure: bool = false,
2624+
suggest_arg: String = String::new(),
26352625
tcx: TyCtxt<'tcx>,
2636-
closure_local_id: Option<hir::HirId>,
2637-
closure_call_changes: Vec<(Span, String)>,
2626+
closure_local_id: Option<hir::HirId> = None,
2627+
closure_call_changes: Vec<(Span, String)> = vec![],
26382628
}
26392629
impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> {
26402630
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
@@ -2715,16 +2705,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
27152705
}) = self.infcx.tcx.hir_node(self.mir_hir_id())
27162706
&& let hir::Node::Expr(expr) = self.infcx.tcx.hir_node(body_id.hir_id)
27172707
{
2718-
let mut finder = ExpressionFinder {
2719-
capture_span: *capture_kind_span,
2720-
closure_change_spans: vec![],
2721-
closure_arg_span: None,
2722-
in_closure: false,
2723-
suggest_arg: String::new(),
2724-
closure_local_id: None,
2725-
closure_call_changes: vec![],
2726-
tcx: self.infcx.tcx,
2727-
};
2708+
let mut finder =
2709+
ExpressionFinder { capture_span: *capture_kind_span, tcx: self.infcx.tcx, .. };
27282710
finder.visit_expr(expr);
27292711

27302712
if finder.closure_change_spans.is_empty() || finder.closure_call_changes.is_empty() {

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(internal_features)]
55
#![feature(assert_matches)]
66
#![feature(box_patterns)]
7+
#![feature(default_field_values)]
78
#![feature(file_buffered)]
89
#![feature(if_let_guard)]
910
#![feature(negative_impls)]

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::ty::{
99
};
1010
use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS;
1111
use rustc_span::kw;
12+
use rustc_trait_selection::traits;
1213
use smallvec::SmallVec;
1314
use tracing::{debug, instrument};
1415

@@ -535,9 +536,26 @@ pub(crate) fn check_generic_arg_count(
535536
.map(|param| param.name)
536537
.collect();
537538
if constraint_names == param_names {
539+
let has_assoc_ty_with_same_name =
540+
if let DefKind::Trait = cx.tcx().def_kind(def_id) {
541+
gen_args.constraints.iter().any(|constraint| {
542+
traits::supertrait_def_ids(cx.tcx(), def_id).any(|trait_did| {
543+
cx.probe_trait_that_defines_assoc_item(
544+
trait_did,
545+
ty::AssocTag::Type,
546+
constraint.ident,
547+
)
548+
})
549+
})
550+
} else {
551+
false
552+
};
538553
// We set this to true and delay emitting `WrongNumberOfGenericArgs`
539-
// to provide a succinct error for cases like issue #113073
540-
all_params_are_binded = true;
554+
// to provide a succinct error for cases like issue #113073,
555+
// but only if when we don't have any assoc type with the same name with a
556+
// generic arg. Otherwise it will cause an ICE due to a delayed error because we
557+
// don't have any error other than `WrongNumberOfGenericArgs`.
558+
all_params_are_binded = !has_assoc_ty_with_same_name;
541559
};
542560
}
543561

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
5151
struct HirWfCheck<'tcx> {
5252
tcx: TyCtxt<'tcx>,
5353
predicate: ty::Predicate<'tcx>,
54-
cause: Option<ObligationCause<'tcx>>,
55-
cause_depth: usize,
54+
cause: Option<ObligationCause<'tcx>> = None,
55+
cause_depth: usize = 0,
5656
icx: ItemCtxt<'tcx>,
5757
def_id: LocalDefId,
5858
param_env: ty::ParamEnv<'tcx>,
59-
depth: usize,
59+
depth: usize = 0,
6060
}
6161

6262
impl<'tcx> Visitor<'tcx> for HirWfCheck<'tcx> {
@@ -124,16 +124,8 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
124124
}
125125
}
126126

127-
let mut visitor = HirWfCheck {
128-
tcx,
129-
predicate,
130-
cause: None,
131-
cause_depth: 0,
132-
icx,
133-
def_id,
134-
param_env: tcx.param_env(def_id.to_def_id()),
135-
depth: 0,
136-
};
127+
let param_env = tcx.param_env(def_id.to_def_id());
128+
let mut visitor = HirWfCheck { tcx, predicate, icx, def_id, param_env, .. };
137129

138130
// Get the starting `hir::Ty` using our `WellFormedLoc`.
139131
// We will walk 'into' this type to try to find

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This API is completely unstable and subject to change.
5959
#![allow(rustc::diagnostic_outside_of_impl)]
6060
#![allow(rustc::untranslatable_diagnostic)]
6161
#![feature(assert_matches)]
62+
#![feature(default_field_values)]
6263
#![feature(gen_blocks)]
6364
#![feature(if_let_guard)]
6465
#![feature(iter_intersperse)]

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::sync::Arc;
22

33
use rustc_abi::FieldIdx;
44
use rustc_middle::mir::*;
5-
use rustc_middle::span_bug;
65
use rustc_middle::thir::*;
76
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
87

@@ -160,10 +159,7 @@ impl<'tcx> MatchPairTree<'tcx> {
160159
}
161160

162161
PatKind::Constant { value } => {
163-
// CAUTION: The type of the pattern node (`pattern.ty`) is
164-
// _often_ the same as the type of the const value (`value.ty`),
165-
// but there are some cases where those types differ
166-
// (e.g. when `deref!(..)` patterns interact with `String`).
162+
assert_eq!(pattern.ty, value.ty);
167163

168164
// Classify the constant-pattern into further kinds, to
169165
// reduce the number of ad-hoc type tests needed later on.
@@ -175,16 +171,6 @@ impl<'tcx> MatchPairTree<'tcx> {
175171
} else if pat_ty.is_floating_point() {
176172
PatConstKind::Float
177173
} else if pat_ty.is_str() {
178-
// Deref-patterns can cause string-literal patterns to have
179-
// type `str` instead of the usual `&str`.
180-
if !cx.tcx.features().deref_patterns() {
181-
span_bug!(
182-
pattern.span,
183-
"const pattern has type `str` but deref_patterns is not enabled"
184-
);
185-
}
186-
PatConstKind::String
187-
} else if pat_ty.is_imm_ref_str() {
188174
PatConstKind::String
189175
} else {
190176
// FIXME(Zalathar): This still covers several different

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,19 +1339,13 @@ enum TestKind<'tcx> {
13391339

13401340
/// Tests the place against a string constant using string equality.
13411341
StringEq {
1342-
/// Constant `&str` value to test against.
1342+
/// Constant string value to test against.
1343+
/// Note that this value has type `str` (not `&str`).
13431344
value: ty::Value<'tcx>,
1344-
/// Type of the corresponding pattern node. Usually `&str`, but could
1345-
/// be `str` for patterns like `deref!("..."): String`.
1346-
pat_ty: Ty<'tcx>,
13471345
},
13481346

13491347
/// Tests the place against a constant using scalar equality.
1350-
ScalarEq {
1351-
value: ty::Value<'tcx>,
1352-
/// Type of the corresponding pattern node.
1353-
pat_ty: Ty<'tcx>,
1354-
},
1348+
ScalarEq { value: ty::Value<'tcx> },
13551349

13561350
/// Test whether the value falls within an inclusive or exclusive range.
13571351
Range(Arc<PatRange<'tcx>>),

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use std::sync::Arc;
99

1010
use rustc_data_structures::fx::FxIndexMap;
1111
use rustc_hir::{LangItem, RangeEnd};
12+
use rustc_middle::bug;
1213
use rustc_middle::mir::*;
1314
use rustc_middle::ty::util::IntTypeExt;
1415
use rustc_middle::ty::{self, GenericArg, Ty, TyCtxt};
15-
use rustc_middle::{bug, span_bug};
1616
use rustc_span::def_id::DefId;
1717
use rustc_span::source_map::Spanned;
1818
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
@@ -39,10 +39,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3939
TestKind::SwitchInt
4040
}
4141
TestableCase::Constant { value, kind: PatConstKind::String } => {
42-
TestKind::StringEq { value, pat_ty: match_pair.pattern_ty }
42+
TestKind::StringEq { value }
4343
}
4444
TestableCase::Constant { value, kind: PatConstKind::Float | PatConstKind::Other } => {
45-
TestKind::ScalarEq { value, pat_ty: match_pair.pattern_ty }
45+
TestKind::ScalarEq { value }
4646
}
4747

4848
TestableCase::Range(ref range) => {
@@ -141,47 +141,33 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
141141
self.cfg.terminate(block, self.source_info(match_start_span), terminator);
142142
}
143143

144-
TestKind::StringEq { value, pat_ty } => {
144+
TestKind::StringEq { value } => {
145145
let tcx = self.tcx;
146146
let success_block = target_block(TestBranch::Success);
147147
let fail_block = target_block(TestBranch::Failure);
148148

149-
let expected_value_ty = value.ty;
150-
let expected_value_operand =
151-
self.literal_operand(test.span, Const::from_ty_value(tcx, value));
149+
let ref_str_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, tcx.types.str_);
150+
assert!(ref_str_ty.is_imm_ref_str(), "{ref_str_ty:?}");
152151

153-
let mut actual_value_ty = pat_ty;
154-
let mut actual_value_place = place;
155-
156-
match pat_ty.kind() {
157-
ty::Str => {
158-
// String literal patterns may have type `str` if `deref_patterns` is
159-
// enabled, in order to allow `deref!("..."): String`. In this case, `value`
160-
// is of type `&str`, so we compare it to `&place`.
161-
if !tcx.features().deref_patterns() {
162-
span_bug!(
163-
test.span,
164-
"matching on `str` went through without enabling deref_patterns"
165-
);
166-
}
167-
let re_erased = tcx.lifetimes.re_erased;
168-
let ref_str_ty = Ty::new_imm_ref(tcx, re_erased, tcx.types.str_);
169-
let ref_place = self.temp(ref_str_ty, test.span);
170-
// `let ref_place: &str = &place;`
171-
self.cfg.push_assign(
172-
block,
173-
self.source_info(test.span),
174-
ref_place,
175-
Rvalue::Ref(re_erased, BorrowKind::Shared, place),
176-
);
177-
actual_value_place = ref_place;
178-
actual_value_ty = ref_str_ty;
179-
}
180-
_ => {}
181-
}
152+
// The string constant we're testing against has type `str`, but
153+
// calling `<str as PartialEq>::eq` requires `&str` operands.
154+
//
155+
// Because `str` and `&str` have the same valtree representation,
156+
// we can "cast" to the desired type by just replacing the type.
157+
assert!(value.ty.is_str(), "unexpected value type for StringEq test: {value:?}");
158+
let expected_value = ty::Value { ty: ref_str_ty, valtree: value.valtree };
159+
let expected_value_operand =
160+
self.literal_operand(test.span, Const::from_ty_value(tcx, expected_value));
182161

183-
assert_eq!(expected_value_ty, actual_value_ty);
184-
assert!(actual_value_ty.is_imm_ref_str());
162+
// Similarly, the scrutinized place has type `str`, but we need `&str`.
163+
// Get a reference by doing `let actual_value_ref_place: &str = &place`.
164+
let actual_value_ref_place = self.temp(ref_str_ty, test.span);
165+
self.cfg.push_assign(
166+
block,
167+
self.source_info(test.span),
168+
actual_value_ref_place,
169+
Rvalue::Ref(tcx.lifetimes.re_erased, BorrowKind::Shared, place),
170+
);
185171

186172
// Compare two strings using `<str as std::cmp::PartialEq>::eq`.
187173
// (Interestingly this means that exhaustiveness analysis relies, for soundness,
@@ -192,11 +178,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
192178
fail_block,
193179
source_info,
194180
expected_value_operand,
195-
Operand::Copy(actual_value_place),
181+
Operand::Copy(actual_value_ref_place),
196182
);
197183
}
198184

199-
TestKind::ScalarEq { value, pat_ty } => {
185+
TestKind::ScalarEq { value } => {
200186
let tcx = self.tcx;
201187
let success_block = target_block(TestBranch::Success);
202188
let fail_block = target_block(TestBranch::Failure);
@@ -205,12 +191,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
205191
let mut expected_value_operand =
206192
self.literal_operand(test.span, Const::from_ty_value(tcx, value));
207193

208-
let mut actual_value_ty = pat_ty;
209194
let mut actual_value_place = place;
210195

211-
match pat_ty.kind() {
196+
match value.ty.kind() {
212197
&ty::Pat(base, _) => {
213-
assert_eq!(pat_ty, value.ty);
214198
assert!(base.is_trivially_pure_clone_copy());
215199

216200
let transmuted_place = self.temp(base, test.span);
@@ -234,15 +218,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
234218
);
235219

236220
actual_value_place = transmuted_place;
237-
actual_value_ty = base;
238221
expected_value_operand = Operand::Copy(transmuted_expect);
239222
expected_value_ty = base;
240223
}
241224
_ => {}
242225
}
243226

244-
assert_eq!(expected_value_ty, actual_value_ty);
245-
assert!(actual_value_ty.is_scalar());
227+
assert!(expected_value_ty.is_scalar());
246228

247229
self.compare(
248230
block,

0 commit comments

Comments
 (0)