Skip to content
Open
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
46 changes: 22 additions & 24 deletions compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let find_param_matching = |matches: &dyn Fn(ParamTerm) -> bool| {
predicate_args.iter().find_map(|arg| {
arg.walk().find_map(|arg| {
if let ty::GenericArgKind::Type(ty) = arg.kind()
&& let ty::Param(param_ty) = *ty.kind()
&& matches(ParamTerm::Ty(param_ty))
{
Some(arg)
} else if let ty::GenericArgKind::Const(ct) = arg.kind()
&& let ty::ConstKind::Param(param_ct) = ct.kind()
&& matches(ParamTerm::Const(param_ct))
arg.walk().find(|arg| match arg.kind() {
ty::GenericArgKind::Type(ty) if let ty::Param(param_ty) = ty.kind() => {
matches(ParamTerm::Ty(*param_ty))
}
ty::GenericArgKind::Const(ct)
if let ty::ConstKind::Param(param_ct) = ct.kind() =>
{
Some(arg)
} else {
None
matches(ParamTerm::Const(param_ct))
}
_ => false,
})
})
};
Expand Down Expand Up @@ -162,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.into_iter()
.flatten()
{
if self.point_at_path_if_possible(error, def_id, param, &qpath) {
if self.point_at_path_if_possible(error, def_id, param, qpath) {
return true;
}
}
Expand Down Expand Up @@ -194,7 +190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
args,
) => {
if let Some(param) = predicate_self_type_to_point_at
&& self.point_at_path_if_possible(error, callee_def_id, param, &qpath)
&& self.point_at_path_if_possible(error, callee_def_id, param, qpath)
{
return true;
}
Expand Down Expand Up @@ -225,7 +221,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.into_iter()
.flatten()
{
if self.point_at_path_if_possible(error, callee_def_id, param, &qpath) {
if self.point_at_path_if_possible(error, callee_def_id, param, qpath) {
return true;
}
}
Expand Down Expand Up @@ -544,10 +540,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

/// - `blame_specific_*` means that the function will recursively traverse the expression,
/// looking for the most-specific-possible span to blame.
/// looking for the most-specific-possible span to blame.
///
/// - `point_at_*` means that the function will only go "one level", pointing at the specific
/// expression mentioned.
/// expression mentioned.
///
/// `blame_specific_arg_if_possible` will find the most-specific expression anywhere inside
/// the provided function call expression, and mark it as responsible for the fulfillment
Expand Down Expand Up @@ -607,9 +603,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/**
* Recursively searches for the most-specific blameable expression.
* For example, if you have a chain of constraints like:
* - want `Vec<i32>: Copy`
* - because `Option<Vec<i32>>: Copy` needs `Vec<i32>: Copy` because `impl <T: Copy> Copy for Option<T>`
* - because `(Option<Vec<i32>, bool)` needs `Option<Vec<i32>>: Copy` because `impl <A: Copy, B: Copy> Copy for (A, B)`
* - want `Vec<i32>: Copy`
* - because `Option<Vec<i32>>: Copy` needs `Vec<i32>: Copy` because `impl <T: Copy> Copy for Option<T>`
* - because `(Option<Vec<i32>, bool)` needs `Option<Vec<i32>>: Copy` because `impl <A: Copy, B: Copy> Copy for (A, B)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single space indent here seems worse.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well, I will revert such changes.

*
* then if you pass in `(Some(vec![1, 2, 3]), false)`, this helper `point_at_specific_expr_if_possible`
* will find the expression `vec![1, 2, 3]` as the "most blameable" reason for this missing constraint.
*
Expand Down Expand Up @@ -747,9 +744,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

/// Drills into `expr` to arrive at the equivalent location of `find_generic_param` in `in_ty`.
/// For example, given
/// - expr: `(Some(vec![1, 2, 3]), false)`
/// - param: `T`
/// - in_ty: `(Option<Vec<T>, bool)`
/// - expr: `(Some(vec![1, 2, 3]), false)`
/// - param: `T`
/// - in_ty: `(Option<Vec<T>, bool)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

///
/// we would drill until we arrive at `vec![1, 2, 3]`.
///
/// If successful, we return `Ok(refined_expr)`. If unsuccessful, we return `Err(partially_refined_expr`),
Expand Down Expand Up @@ -1017,7 +1015,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.variant_with_id(variant_def_id)
.fields
.iter()
.map(|field| field.ty(self.tcx, *in_ty_adt_generic_args))
.map(|field| field.ty(self.tcx, in_ty_adt_generic_args))
.enumerate()
.filter(|(_index, field_type)| find_param_in_ty((*field_type).into(), param)),
) else {
Expand Down
36 changes: 13 additions & 23 deletions compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'tcx> ArgMatrix<'tcx> {
return Some(Issue::Missing(next_unmatched_idx));
}
// If we eliminate the last column, any left-over inputs are extra
if mat[i].len() == 0 {
if mat[i].is_empty() {
return Some(Issue::Extra(next_unmatched_idx));
}

Expand All @@ -187,28 +187,18 @@ impl<'tcx> ArgMatrix<'tcx> {
continue;
}

let mut useless = true;
let mut unsatisfiable = true;
if is_arg {
for j in 0..ii.len() {
// If we find at least one input this argument could satisfy
// this argument isn't unsatisfiable
if matches!(mat[j][i], Compatibility::Compatible) {
unsatisfiable = false;
break;
}
}
}
if is_input {
for j in 0..ai.len() {
// If we find at least one argument that could satisfy this input
// this input isn't useless
if matches!(mat[i][j], Compatibility::Compatible) {
useless = false;
break;
}
}
}
// If this argument can satisfy some input, then this argument is satisfiable
let unsatisfiable = if is_arg {
!mat.iter().take(ii.len()).any(|c| matches!(c[i], Compatibility::Compatible))
} else {
true
};
// If this input can be satisfied by some argument, then this input is useful
let useless = if is_input {
!mat[i].iter().take(ai.len()).any(|c| matches!(c, Compatibility::Compatible))
} else {
true
};

match (is_input, is_arg, useless, unsatisfiable) {
// If an argument is unsatisfied, and the input in its position is useless
Expand Down
21 changes: 7 additions & 14 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,10 +780,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
_ => bug!("unexpected type: {:?}", ty.normalized),
},
Res::Def(
DefKind::Struct | DefKind::Union | DefKind::TyAlias { .. } | DefKind::AssocTy,
_,
)
Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. } => match ty.normalized.ty_adt_def() {
Some(adt) if !adt.is_enum() => {
Expand Down Expand Up @@ -871,7 +868,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let decl_ty = self.local_ty(decl.span, decl.hir_id);

// Type check the initializer.
if let Some(ref init) = decl.init {
if let Some(init) = decl.init {
let init_ty = self.check_decl_initializer(decl.hir_id, decl.pat, init);
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, init_ty);
}
Expand Down Expand Up @@ -935,7 +932,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
// Ignore for now.
hir::StmtKind::Item(_) => {}
hir::StmtKind::Expr(ref expr) => {
hir::StmtKind::Expr(expr) => {
// Check with expected type of `()`.
self.check_expr_has_type_or_error(expr, self.tcx.types.unit, |err| {
if self.is_next_stmt_expr_continuation(stmt.hir_id)
Expand Down Expand Up @@ -1774,10 +1771,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let params =
params.get(is_method as usize..params.len() - sig.decl.c_variadic as usize)?;
debug_assert_eq!(params.len(), fn_inputs.len());
Some((
fn_inputs.zip(params.iter().map(|param| FnParam::Param(param))).collect(),
generics,
))
Some((fn_inputs.zip(params.iter().map(FnParam::Param)).collect(), generics))
}
(None, Some(params)) => {
let params =
Expand Down Expand Up @@ -2639,7 +2633,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
suggestions: Vec<(Span, String)>,
suggestion_text: SuggestionText,
) -> Option<String> {
let suggestion_text = match suggestion_text {
match suggestion_text {
SuggestionText::None => None,
SuggestionText::Provide(plural) => {
Some(format!("provide the argument{}", if plural { "s" } else { "" }))
Expand All @@ -2655,8 +2649,7 @@ impl<'a, 'b, 'tcx> FnCallDiagCtxt<'a, 'b, 'tcx> {
SuggestionText::Swap => Some("swap these arguments".to_string()),
SuggestionText::Reorder => Some("reorder these arguments".to_string()),
SuggestionText::DidYouMean => Some("did you mean".to_string()),
};
suggestion_text
}
}

fn arguments_formatting(&self, suggestion_span: Span) -> ArgumentsFormatting {
Expand Down Expand Up @@ -2954,7 +2947,7 @@ impl<'a, 'b, 'tcx> ArgsCtxt<'a, 'b, 'tcx> {
.fn_ctxt
.typeck_results
.borrow()
.expr_ty_adjusted_opt(*expr)
.expr_ty_adjusted_opt(expr)
.unwrap_or_else(|| Ty::new_misc_error(self.call_ctxt.fn_ctxt.tcx));
(
self.call_ctxt.fn_ctxt.resolve_vars_if_possible(ty),
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
})
.collect::<Vec<String>>();

if all_matching_bounds_strs.len() == 0 {
if all_matching_bounds_strs.is_empty() {
return;
}

Expand Down Expand Up @@ -1336,7 +1336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected_ty = *inner_expected_ty;
}
(hir::ExprKind::Block(blk, _), _, _) => {
self.suggest_block_to_brackets(diag, *blk, expr_ty, expected_ty);
self.suggest_block_to_brackets(diag, blk, expr_ty, expected_ty);
break true;
}
_ => break false,
Expand Down Expand Up @@ -1463,7 +1463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
let span = expr
.span
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
.find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
.unwrap_or(expr.span);

let mut sugg = if self.precedence(expr) >= ExprPrecedence::Unambiguous {
Expand Down Expand Up @@ -2133,7 +2133,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let span = expr
.span
.find_ancestor_not_from_extern_macro(&self.tcx.sess.source_map())
.find_ancestor_not_from_extern_macro(self.tcx.sess.source_map())
.unwrap_or(expr.span);
err.span_suggestion_verbose(span.shrink_to_hi(), msg, sugg, Applicability::HasPlaceholders);
true
Expand Down Expand Up @@ -2246,7 +2246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let src_map = tcx.sess.source_map();
let suggestion = if src_map.is_multiline(expr.span) {
let indentation = src_map.indentation_before(span).unwrap_or_else(String::new);
let indentation = src_map.indentation_before(span).unwrap_or_default();
format!("\n{indentation}/* {suggestion} */")
} else {
// If the entire expr is on a single line
Expand Down Expand Up @@ -2278,7 +2278,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Check if `expr` is contained in array of two elements
if let hir::Node::Expr(array_expr) = self.tcx.parent_hir_node(expr.hir_id)
&& let hir::ExprKind::Array(elements) = array_expr.kind
&& let [first, second] = &elements[..]
&& let [first, second] = elements
&& second.hir_id == expr.hir_id
{
// Span between the two elements of the array
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/gather_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
/// again during type checking by querying [`FnCtxt::local_ty`] for the same hir_id.
fn declare(&mut self, decl: Declaration<'tcx>) {
let local_ty = match decl.ty {
Some(ref ty) => {
Some(ty) => {
let o_ty = self.fcx.lower_ty(ty);

let c_ty = self.fcx.infcx.canonicalize_user_type_annotation(
Expand Down
17 changes: 6 additions & 11 deletions compiler/rustc_hir_typeck/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,15 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
}
}
}
hir::ExprKind::Loop(ref b, _, source, _) => {
hir::ExprKind::Loop(b, _, source, _) => {
let cx = match self.is_loop_match(e, b) {
Some(labeled_block) => LoopMatch { labeled_block },
None => Loop(source),
};

self.with_context(cx, |v| v.visit_block(b));
}
hir::ExprKind::Closure(&hir::Closure {
ref fn_decl, body, fn_decl_span, kind, ..
}) => {
hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, kind, .. }) => {
let cx = match kind {
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(kind, source)) => {
Coroutine { coroutine_span: fn_decl_span, kind, source }
Expand All @@ -167,16 +165,16 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
self.visit_fn_decl(fn_decl);
self.with_context(cx, |v| v.visit_nested_body(body));
}
hir::ExprKind::Block(ref b, Some(_label)) => {
hir::ExprKind::Block(b, Some(_label)) => {
self.with_context(LabeledBlock, |v| v.visit_block(b));
}
hir::ExprKind::Block(ref b, None)
hir::ExprKind::Block(b, None)
if matches!(self.cx_stack.last(), Some(&Fn) | Some(&ConstBlock)) =>
{
self.with_context(Normal, |v| v.visit_block(b));
}
hir::ExprKind::Block(
ref b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. },
b @ hir::Block { rules: hir::BlockCheckMode::DefaultBlock, .. },
None,
) if matches!(
self.cx_stack.last(),
Expand Down Expand Up @@ -431,10 +429,7 @@ impl<'hir> CheckLoopVisitor<'hir> {

// Accept either `state = expr` or `state = expr;`.
let loop_body_expr = match body.stmts {
[] => match body.expr {
Some(expr) => expr,
None => return None,
},
[] => body.expr?,
[single] if body.expr.is_none() => match single.kind {
hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => expr,
_ => return None,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_index/src/bit_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
BitMatrix {
num_rows,
num_columns,
words: iter::repeat(&row.words).take(num_rows).flatten().cloned().collect(),
words: iter::repeat_n(&row.words, num_rows).flatten().cloned().collect(),
marker: PhantomData,
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_transform/src/impossible_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
//! parameters, so this filtering serves two purposes:
//!
//! 1. We skip evaluating any predicates that we would
//! never be able prove are unsatisfiable (e.g. `<T as Foo>`
//! never be able prove are unsatisfiable (e.g. `<T as Foo>`
//! 2. We avoid trying to normalize predicates involving generic
//! parameters (e.g. `<T as Foo>::MyItem`). This can confuse
//! the normalization code (leading to cycle errors), since
//! it's usually never invoked in this way.
//! parameters (e.g. `<T as Foo>::MyItem`). This can confuse
//! the normalization code (leading to cycle errors), since
//! it's usually never invoked in this way.

use rustc_middle::mir::{Body, START_BLOCK, TerminatorKind};
use rustc_middle::ty::{TyCtxt, TypeFlags, TypeVisitableExt};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ fn try_inlining<'tcx, I: Inliner<'tcx>>(

// Normally, this shouldn't be required, but trait normalization failure can create a
// validation ICE.
if !validate_types(tcx, inliner.typing_env(), &callee_body, &caller_body).is_empty() {
if !validate_types(tcx, inliner.typing_env(), &callee_body, caller_body).is_empty() {
debug!("failed to validate callee body");
return Err("implementation limitation -- callee body failed validation");
}
Expand Down
Loading