Skip to content

Commit 4240a0b

Browse files
committed
Cleanup PatCtxt.
1 parent 05082f5 commit 4240a0b

File tree

2 files changed

+23
-73
lines changed

2 files changed

+23
-73
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ struct ConstToPat<'tcx> {
5959
// inference context used for checking `T: Structural` bounds.
6060
infcx: InferCtxt<'tcx>,
6161

62-
include_lint_checks: bool,
63-
6462
treat_byte_string_as_slice: bool,
6563
}
6664

@@ -93,7 +91,6 @@ impl<'tcx> ConstToPat<'tcx> {
9391
span,
9492
infcx,
9593
param_env: pat_ctxt.param_env,
96-
include_lint_checks: pat_ctxt.include_lint_checks,
9794
saw_const_match_error: Cell::new(false),
9895
saw_const_match_lint: Cell::new(false),
9996
behind_reference: Cell::new(false),
@@ -134,7 +131,7 @@ impl<'tcx> ConstToPat<'tcx> {
134131
})
135132
});
136133

137-
if self.include_lint_checks && !self.saw_const_match_error.get() {
134+
if !self.saw_const_match_error.get() {
138135
// If we were able to successfully convert the const to some pat,
139136
// double-check that all types in the const implement `Structural`.
140137

@@ -239,21 +236,19 @@ impl<'tcx> ConstToPat<'tcx> {
239236

240237
let kind = match cv.ty().kind() {
241238
ty::Float(_) => {
242-
if self.include_lint_checks {
243239
tcx.emit_spanned_lint(
244240
lint::builtin::ILLEGAL_FLOATING_POINT_LITERAL_PATTERN,
245241
id,
246242
span,
247243
FloatPattern,
248244
);
249-
}
250245
PatKind::Constant { value: cv }
251246
}
252247
ty::Adt(adt_def, _) if adt_def.is_union() => {
253248
// Matching on union fields is unsafe, we can't hide it in constants
254249
self.saw_const_match_error.set(true);
255250
let err = UnionPattern { span };
256-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
251+
tcx.sess.emit_err(err);
257252
PatKind::Wild
258253
}
259254
ty::Adt(..)
@@ -267,7 +262,7 @@ impl<'tcx> ConstToPat<'tcx> {
267262
{
268263
self.saw_const_match_error.set(true);
269264
let err = TypeNotStructural { span, non_sm_ty };
270-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
265+
tcx.sess.emit_err(err);
271266
PatKind::Wild
272267
}
273268
// If the type is not structurally comparable, just emit the constant directly,
@@ -280,8 +275,7 @@ impl<'tcx> ConstToPat<'tcx> {
280275
// Backwards compatibility hack because we can't cause hard errors on these
281276
// types, so we compare them via `PartialEq::eq` at runtime.
282277
ty::Adt(..) if !self.type_marked_structural(cv.ty()) && self.behind_reference.get() => {
283-
if self.include_lint_checks
284-
&& !self.saw_const_match_error.get()
278+
if !self.saw_const_match_error.get()
285279
&& !self.saw_const_match_lint.get()
286280
{
287281
self.saw_const_match_lint.set(true);
@@ -305,7 +299,7 @@ impl<'tcx> ConstToPat<'tcx> {
305299
);
306300
self.saw_const_match_error.set(true);
307301
let err = TypeNotStructural { span, non_sm_ty: cv.ty() };
308-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
302+
tcx.sess.emit_err(err);
309303
PatKind::Wild
310304
}
311305
ty::Adt(adt_def, substs) if adt_def.is_enum() => {
@@ -339,7 +333,7 @@ impl<'tcx> ConstToPat<'tcx> {
339333
ty::Dynamic(..) => {
340334
self.saw_const_match_error.set(true);
341335
let err = InvalidPattern { span, non_sm_ty: cv.ty() };
342-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
336+
tcx.sess.emit_err(err);
343337
PatKind::Wild
344338
}
345339
// `&str` is represented as `ConstValue::Slice`, let's keep using this
@@ -406,8 +400,7 @@ impl<'tcx> ConstToPat<'tcx> {
406400
// to figure out how to get a reference again.
407401
ty::Adt(_, _) if !self.type_marked_structural(*pointee_ty) => {
408402
if self.behind_reference.get() {
409-
if self.include_lint_checks
410-
&& !self.saw_const_match_error.get()
403+
if !self.saw_const_match_error.get()
411404
&& !self.saw_const_match_lint.get()
412405
{
413406
self.saw_const_match_lint.set(true);
@@ -423,7 +416,7 @@ impl<'tcx> ConstToPat<'tcx> {
423416
if !self.saw_const_match_error.get() {
424417
self.saw_const_match_error.set(true);
425418
let err = TypeNotStructural { span, non_sm_ty: *pointee_ty };
426-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
419+
tcx.sess.emit_err(err);
427420
}
428421
PatKind::Wild
429422
}
@@ -437,7 +430,7 @@ impl<'tcx> ConstToPat<'tcx> {
437430
// (except slices, which are handled in a separate arm above).
438431

439432
let err = UnsizedPattern { span, non_sm_ty: *pointee_ty };
440-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
433+
tcx.sess.emit_err(err);
441434

442435
PatKind::Wild
443436
} else {
@@ -465,8 +458,7 @@ impl<'tcx> ConstToPat<'tcx> {
465458
// compilation choices change the runtime behaviour of the match.
466459
// See https://github.com/rust-lang/rust/issues/70861 for examples.
467460
ty::FnPtr(..) | ty::RawPtr(..) => {
468-
if self.include_lint_checks
469-
&& !self.saw_const_match_error.get()
461+
if !self.saw_const_match_error.get()
470462
&& !self.saw_const_match_lint.get()
471463
{
472464
self.saw_const_match_lint.set(true);
@@ -482,13 +474,12 @@ impl<'tcx> ConstToPat<'tcx> {
482474
_ => {
483475
self.saw_const_match_error.set(true);
484476
let err = InvalidPattern { span, non_sm_ty: cv.ty() };
485-
tcx.sess.create_err(err).emit_unless(!self.include_lint_checks);
477+
tcx.sess.emit_err(err);
486478
PatKind::Wild
487479
}
488480
};
489481

490-
if self.include_lint_checks
491-
&& !self.saw_const_match_error.get()
482+
if !self.saw_const_match_error.get()
492483
&& !self.saw_const_match_lint.get()
493484
&& mir_structural_match_violation
494485
// FIXME(#73448): Find a way to bring const qualification into parity with

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

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,10 @@ use rustc_target::abi::FieldIdx;
3131

3232
use std::cmp::Ordering;
3333

34-
#[derive(Clone, Debug)]
35-
enum PatternError {
36-
AssocConstInPattern(Span),
37-
ConstParamInPattern(Span),
38-
StaticInPattern(Span),
39-
NonConstPath(Span),
40-
}
41-
4234
struct PatCtxt<'a, 'tcx> {
4335
tcx: TyCtxt<'tcx>,
4436
param_env: ty::ParamEnv<'tcx>,
4537
typeck_results: &'a ty::TypeckResults<'tcx>,
46-
errors: Vec<PatternError>,
47-
include_lint_checks: bool,
4838
}
4939

5040
pub(super) fn pat_from_hir<'a, 'tcx>(
@@ -53,47 +43,13 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
5343
typeck_results: &'a ty::TypeckResults<'tcx>,
5444
pat: &'tcx hir::Pat<'tcx>,
5545
) -> Box<Pat<'tcx>> {
56-
let mut pcx = PatCtxt::new(tcx, param_env, typeck_results);
57-
pcx.include_lint_checks();
46+
let mut pcx = PatCtxt { tcx, param_env, typeck_results };
5847
let result = pcx.lower_pattern(pat);
59-
pcx.report_inlining_errors();
6048
debug!("pat_from_hir({:?}) = {:?}", pat, result);
6149
result
6250
}
6351

6452
impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
65-
fn new(
66-
tcx: TyCtxt<'tcx>,
67-
param_env: ty::ParamEnv<'tcx>,
68-
typeck_results: &'a ty::TypeckResults<'tcx>,
69-
) -> Self {
70-
PatCtxt { tcx, param_env, typeck_results, errors: vec![], include_lint_checks: false }
71-
}
72-
73-
fn include_lint_checks(&mut self) -> &mut Self {
74-
self.include_lint_checks = true;
75-
self
76-
}
77-
78-
fn report_inlining_errors(&self) {
79-
for error in &self.errors {
80-
match *error {
81-
PatternError::StaticInPattern(span) => {
82-
self.tcx.sess.emit_err(StaticInPattern { span });
83-
}
84-
PatternError::AssocConstInPattern(span) => {
85-
self.tcx.sess.emit_err(AssocConstInPattern { span });
86-
}
87-
PatternError::ConstParamInPattern(span) => {
88-
self.tcx.sess.emit_err(ConstParamInPattern { span });
89-
}
90-
PatternError::NonConstPath(span) => {
91-
self.tcx.sess.emit_err(NonConstPath { span });
92-
}
93-
}
94-
}
95-
}
96-
9753
fn lower_pattern(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
9854
// When implicit dereferences have been inserted in this pattern, the unadjusted lowered
9955
// pattern has the type that results *after* dereferencing. For example, in this code:
@@ -490,12 +446,15 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
490446
| Res::SelfTyAlias { .. }
491447
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
492448
_ => {
493-
let pattern_error = match res {
494-
Res::Def(DefKind::ConstParam, _) => PatternError::ConstParamInPattern(span),
495-
Res::Def(DefKind::Static(_), _) => PatternError::StaticInPattern(span),
496-
_ => PatternError::NonConstPath(span),
449+
match res {
450+
Res::Def(DefKind::ConstParam, _) => {
451+
self.tcx.sess.emit_err(ConstParamInPattern { span })
452+
}
453+
Res::Def(DefKind::Static(_), _) => {
454+
self.tcx.sess.emit_err(StaticInPattern { span })
455+
}
456+
_ => self.tcx.sess.emit_err(NonConstPath { span }),
497457
};
498-
self.errors.push(pattern_error);
499458
PatKind::Wild
500459
}
501460
};
@@ -548,7 +507,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
548507
// It should be assoc consts if there's no error but we cannot resolve it.
549508
debug_assert!(is_associated_const);
550509

551-
self.errors.push(PatternError::AssocConstInPattern(span));
510+
self.tcx.sess.emit_err(AssocConstInPattern { span });
552511

553512
return pat_from_kind(PatKind::Wild);
554513
}
@@ -626,7 +585,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
626585
match value {
627586
mir::ConstantKind::Ty(c) => match c.kind() {
628587
ConstKind::Param(_) => {
629-
self.errors.push(PatternError::ConstParamInPattern(span));
588+
self.tcx.sess.emit_err(ConstParamInPattern { span });
630589
return PatKind::Wild;
631590
}
632591
ConstKind::Error(_) => {

0 commit comments

Comments
 (0)