Skip to content
Merged
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
72 changes: 33 additions & 39 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ You can read more about trait objects in the Trait Objects section of the Refere
https://doc.rust-lang.org/reference/types.html#trait-objects";

fn is_number(text: &str) -> bool {
text.chars().all(|c: char| c.is_digit(10))
text.chars().all(|c: char| c.is_ascii_digit())
}

/// Information about the expected type at the top level of type checking a pattern.
Expand Down Expand Up @@ -262,8 +262,9 @@ enum InheritedRefMatchRule {
/// pattern matches a given type:
/// - If the underlying type is not a reference, a reference pattern may eat the inherited reference;
/// - If the underlying type is a reference, a reference pattern matches if it can eat either one
/// of the underlying and inherited references. E.g. a `&mut` pattern is allowed if either the
/// underlying type is `&mut` or the inherited reference is `&mut`.
/// of the underlying and inherited references. E.g. a `&mut` pattern is allowed if either the
/// underlying type is `&mut` or the inherited reference is `&mut`.
///
/// If `false`, a reference pattern is only matched against the underlying type.
/// This is `false` for stable Rust and `true` for both the `ref_pat_eat_one_layer_2024` and
/// `ref_pat_eat_one_layer_2024_structural` feature gates.
Expand Down Expand Up @@ -1069,7 +1070,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
if !self.tcx.features().mut_ref() {
feature_err(
&self.tcx.sess,
self.tcx.sess,
sym::mut_ref,
pat.span.until(ident.span),
"binding cannot be both mutable and by-reference",
Expand Down Expand Up @@ -1487,31 +1488,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
opt_def_id: Option<hir::def_id::DefId>,
ident: Ident,
) -> bool {
match opt_def_id {
Some(def_id) => match self.tcx.hir_get_if_local(def_id) {
Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Const(_, _, _, body_id),
..
})) => match self.tcx.hir_node(body_id.hir_id) {
hir::Node::Expr(expr) => {
if hir::is_range_literal(expr) {
let span = self.tcx.hir_span(body_id.hir_id);
if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) {
e.span_suggestion_verbose(
ident.span,
"you may want to move the range into the match block",
snip,
Applicability::MachineApplicable,
);
return true;
}
}
}
_ => (),
},
_ => (),
},
_ => (),
if let Some(def_id) = opt_def_id
&& let Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Const(_, _, _, body_id),
..
})) = self.tcx.hir_get_if_local(def_id)
&& let hir::Node::Expr(expr) = self.tcx.hir_node(body_id.hir_id)
&& hir::is_range_literal(expr)
{
let span = self.tcx.hir_span(body_id.hir_id);
if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) {
e.span_suggestion_verbose(
ident.span,
"you may want to move the range into the match block",
snip,
Applicability::MachineApplicable,
);
return true;
}
}
false
}
Expand All @@ -1529,7 +1523,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

if let Some(span) = self.tcx.hir_res_span(pat_res) {
e.span_label(span, format!("{} defined here", res.descr()));
if let [hir::PathSegment { ident, .. }] = &*segments {
if let [hir::PathSegment { ident, .. }] = segments {
e.span_label(
pat_span,
format!(
Expand Down Expand Up @@ -1557,17 +1551,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => (None, None),
};

let is_range = match type_def_id.and_then(|id| self.tcx.as_lang_item(id)) {
let is_range = matches!(
type_def_id.and_then(|id| self.tcx.as_lang_item(id)),
Some(
LangItem::Range
| LangItem::RangeFrom
| LangItem::RangeTo
| LangItem::RangeFull
| LangItem::RangeInclusiveStruct
| LangItem::RangeToInclusive,
) => true,
_ => false,
};
| LangItem::RangeFrom
| LangItem::RangeTo
| LangItem::RangeFull
| LangItem::RangeInclusiveStruct
| LangItem::RangeToInclusive,
)
);
if is_range {
if !self.maybe_suggest_range_literal(&mut e, item_def_id, *ident) {
let msg = "constants only support matching by type, \
Expand Down
Loading