Skip to content
Merged
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
13 changes: 10 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// being stalled on a coroutine.
self.select_obligations_where_possible(|_| {});

let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
else {
bug!();
let defining_opaque_types_and_generators = match self.typing_mode() {
ty::TypingMode::Analysis { defining_opaque_types_and_generators } => {
defining_opaque_types_and_generators
}
ty::TypingMode::Coherence
| ty::TypingMode::Borrowck { .. }
| ty::TypingMode::PostBorrowckAnalysis { .. }
| ty::TypingMode::PostAnalysis => {
bug!()
}
};

if defining_opaque_types_and_generators
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_hir_typeck/src/opaque_types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rustc_hir::def::DefKind;
use rustc_infer::traits::ObligationCause;
use rustc_middle::bug;
use rustc_middle::ty::{
self, DefiningScopeKind, DefinitionSiteHiddenType, OpaqueTypeKey, ProvisionalHiddenType,
TypeVisitableExt, TypingMode,
TypeVisitableExt,
};
use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
use rustc_trait_selection::opaque_types::{
Expand Down Expand Up @@ -97,9 +98,16 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
debug!(?opaque_types);

let tcx = self.tcx;
let TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()
else {
unreachable!();
let defining_opaque_types_and_generators = match self.typing_mode() {
ty::TypingMode::Analysis { defining_opaque_types_and_generators } => {
defining_opaque_types_and_generators
}
ty::TypingMode::Coherence
| ty::TypingMode::Borrowck { .. }
| ty::TypingMode::PostBorrowckAnalysis { .. }
| ty::TypingMode::PostAnalysis => {
bug!()
}
};

for def_id in defining_opaque_types_and_generators {
Expand Down
26 changes: 24 additions & 2 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,37 @@ impl<'tcx> LateLintPass<'tcx> for RustcMustMatchExhaustively {
}
}
}
hir::ExprKind::If(expr, ..) if let ExprKind::Let(expr) = expr.kind => {
hir::ExprKind::Let(expr, ..) => {
if let Some(attr_span) = is_rustc_must_match_exhaustively(cx, expr.init.hir_id) {
cx.emit_span_lint(
RUSTC_MUST_MATCH_EXHAUSTIVELY,
expr.span,
RustcMustMatchExhaustivelyNotExhaustive {
attr_span,
pat_span: expr.span,
message: "using if let only matches on one variant (try using `match`)",
message: "using `if let` only matches on one variant (try using `match`)",
},
);
}
}
_ => {}
}
}

fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx rustc_hir::Stmt<'tcx>) {
match stmt.kind {
rustc_hir::StmtKind::Let(let_stmt) => {
if let_stmt.els.is_some()
&& let Some(attr_span) =
is_rustc_must_match_exhaustively(cx, let_stmt.pat.hir_id)
{
cx.emit_span_lint(
RUSTC_MUST_MATCH_EXHAUSTIVELY,
let_stmt.span,
RustcMustMatchExhaustivelyNotExhaustive {
attr_span,
pat_span: let_stmt.pat.span,
message: "using `let else` only matches on one variant (try using `match`)",
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ where
// normalizing the self type as well, since type variables are not uniquified.
let goal = self.resolve_vars_if_possible(goal);

if let TypingMode::Coherence = self.typing_mode()
if self.typing_mode().is_coherence()
&& let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
{
candidates.push(candidate);
Expand Down
3 changes: 3 additions & 0 deletions tests/ui-fulldeps/internal-lints/must_match_exhaustively.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ fn foo(f: Foo) {

if let Foo::A { .. } = f {}
//~^ ERROR match is not exhaustive

let Foo::A { .. } = f else { loop {} };
//~^ ERROR match is not exhaustive
}

fn main() {}
20 changes: 18 additions & 2 deletions tests/ui-fulldeps/internal-lints/must_match_exhaustively.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,27 @@ LL | if let Foo::A { .. } = f {}
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: explicitly list all variants of the enum in a `match`
note: using if let only matches on one variant (try using `match`)
note: using `if let` only matches on one variant (try using `match`)
--> $DIR/must_match_exhaustively.rs:44:8
|
LL | if let Foo::A { .. } = f {}
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors
error: match is not exhaustive
--> $DIR/must_match_exhaustively.rs:47:5
|
LL | #[rustc_must_match_exhaustively]
| -------------------------------- required because of this attribute
...
LL | let Foo::A { .. } = f else { loop {} };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: explicitly list all variants of the enum in a `match`
note: using `let else` only matches on one variant (try using `match`)
--> $DIR/must_match_exhaustively.rs:47:9
|
LL | let Foo::A { .. } = f else { loop {} };
| ^^^^^^^^^^^^^

error: aborting due to 5 previous errors

Loading