Skip to content

Commit 7fc8e3c

Browse files
Rust cleanup
In a lot of these cases, I prefer the way I formatted the code, but I'll defer to the tidy script :)
1 parent c266c4c commit 7fc8e3c

File tree

2 files changed

+40
-45
lines changed
  • compiler/rustc_typeck/src/check/fn_ctxt
  • src/test/ui/argument-suggestions

2 files changed

+40
-45
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::check::method::MethodCallee;
44
use crate::check::Expectation::*;
55
use crate::check::TupleArgumentsFlag::*;
66
use crate::check::{
7-
struct_span_err, BreakableCtxt, Diverges, Expectation, FnCtxt,
8-
LocalTy, Needs, TupleArgumentsFlag,
7+
struct_span_err, BreakableCtxt, Diverges, Expectation, FnCtxt, LocalTy, Needs,
8+
TupleArgumentsFlag,
99
};
1010

1111
use rustc_ast as ast;
@@ -19,13 +19,16 @@ use rustc_middle::ty::adjustment::AllowTwoPhase;
1919
use rustc_middle::ty::fold::TypeFoldable;
2020
use rustc_middle::ty::{self, Ty};
2121
use rustc_session::Session;
22-
use rustc_span::{BytePos, Pos, symbol::{sym, Ident}};
2322
use rustc_span::{self, MultiSpan, Span};
23+
use rustc_span::{
24+
symbol::{sym, Ident},
25+
BytePos, Pos,
26+
};
2427
use rustc_trait_selection::traits::{self, ObligationCauseCode, StatementAsExpression};
2528

29+
use std::cmp;
2630
use std::mem::replace;
2731
use std::slice;
28-
use std::cmp;
2932

3033
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3134
pub(in super::super) fn check_casts(&self) {
@@ -96,12 +99,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9699
&self,
97100
call_span: Span, // Span enclosing the call site
98101
call_expr: &'tcx hir::Expr<'tcx>, // Expression of the call site
99-
formal_input_tys: &[Ty<'tcx>], // Types (as defined in the *signature* of the target function)
100-
expected_input_tys: &[Ty<'tcx>], // More specific expected types, after unifying with caller output types
102+
formal_input_tys: &[Ty<'tcx>], // Types (as defined in the *signature* of the target function)
103+
expected_input_tys: &[Ty<'tcx>], // More specific expected types, after unifying with caller output types
101104
provided_args: &'tcx [hir::Expr<'tcx>], // The expressions for each provided argument
102-
c_variadic: bool, // Whether the function is variadic, for example when imported from C
103-
tuple_arguments: TupleArgumentsFlag, // Whether the arguments have been bundled in a tuple (ex: closures)
104-
fn_def_id: Option<DefId>, // The DefId for the function being called, for better error messages
105+
c_variadic: bool, // Whether the function is variadic, for example when imported from C
106+
tuple_arguments: TupleArgumentsFlag, // Whether the arguments have been bundled in a tuple (ex: closures)
107+
fn_def_id: Option<DefId>, // The DefId for the function being called, for better error messages
105108
) {
106109
let tcx = self.tcx;
107110

@@ -198,7 +201,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
198201
let coerced_ty = expectation.only_has_type(self).unwrap_or(formal_input_ty);
199202
let coerced_ty = self.resolve_vars_with_obligations(coerced_ty);
200203

201-
let coerce_error = self.try_coerce(provided_arg, checked_ty, coerced_ty, AllowTwoPhase::Yes);
204+
let coerce_error =
205+
self.try_coerce(provided_arg, checked_ty, coerced_ty, AllowTwoPhase::Yes);
202206

203207
// 3. Check if the formal type is a supertype of the checked one
204208
// and register any such obligations for future type checks
@@ -262,7 +266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
262266
self.warn_if_unreachable(
263267
provided_args[idx].hir_id,
264268
provided_args[idx].span,
265-
"expression"
269+
"expression",
266270
);
267271

268272
// If we're past the end of the expected inputs, we won't have anything to check against
@@ -327,14 +331,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
327331
// First, fill in the rest of our compatibility matrix
328332
for i in 0..provided_arg_count {
329333
for j in 0..minimum_input_count {
330-
if i == j { continue; }
334+
if i == j {
335+
continue;
336+
}
331337
compatibility_matrix[i][j] = check_compatible(i, j);
332338
}
333339
}
334340

335341
// Obviously, detecting exact user intention is impossible, so the goal here is to
336342
// come up with as likely of a story as we can to be helpful.
337-
//
343+
//
338344
// We'll iteratively removed "satisfied" input/argument paris,
339345
// then check for the cases above, until we've eliminated the entire grid
340346
//
@@ -398,7 +404,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
398404
// This is a satisfied input, so move along
399405
continue;
400406
}
401-
407+
402408
let mut useless = true;
403409
let mut unsatisfiable = true;
404410
if is_arg {
@@ -473,12 +479,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
473479
loop {
474480
stack.push(j);
475481
// Look for params this one could slot into
476-
let compat: Vec<_> = mat[j]
477-
.iter()
478-
.enumerate()
479-
.filter(|(_, &c)| c)
480-
.map(|(i, _)| i)
481-
.collect();
482+
let compat: Vec<_> =
483+
mat[j].iter().enumerate().filter(|(_, &c)| c).map(|(i, _)| i).collect();
482484
if compat.len() != 1 {
483485
// this could go into multipl slots, don't bother exploring both
484486
is_cycle = false;
@@ -491,7 +493,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
491493
}
492494
}
493495
if stack.len() <= 2 {
494-
// If we encounter a cycle of 1 or 2 elements, we'll let the
496+
// If we encounter a cycle of 1 or 2 elements, we'll let the
495497
// "satisfy" and "swap" code above handle those
496498
}
497499
// We've built up some chain, some of which might be a cycle
@@ -541,7 +543,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
541543
}
542544
Some(Issue::Missing(idx)) => {
543545
// FIXME: improve these with help from code reviewers
544-
let input_ty = self.resolve_vars_if_possible(expected_input_tys[input_indexes[idx]]);
546+
let input_ty =
547+
self.resolve_vars_if_possible(expected_input_tys[input_indexes[idx]]);
545548
if input_ty.is_unit() {
546549
info!("~~~ Issue: Maybe use ()?"); // FIXME
547550
}
@@ -555,7 +558,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
555558
&mut compatibility_matrix,
556559
&mut input_indexes,
557560
&mut arg_indexes,
558-
min, max,
561+
min,
562+
max,
559563
);
560564
satisfy_input(
561565
&mut compatibility_matrix,
@@ -569,7 +573,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
569573
// FIXME: If satisfy_input ever did anything non-trivial (emit obligations to help type checking, for example)
570574
// we'd want to call this function with the correct arg/input pairs, but for now, we just throw them in a bucket.
571575
// This works because they force a cycle, so each row is guaranteed to also be a column
572-
let mut idxs: Vec<usize> = args.iter().filter(|a| a.is_some()).map(|a| a.unwrap()).collect();
576+
let mut idxs: Vec<usize> =
577+
args.iter().filter(|a| a.is_some()).map(|a| a.unwrap()).collect();
573578
// FIXME: Is there a cleaner way to do this?
574579
let mut real_idxs = vec![None; provided_args.len()];
575580
for (src, dst) in args.iter().enumerate() {
@@ -591,7 +596,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
591596
None => {
592597
// We didn't find any issues, so we need to push the algorithm forward
593598
// First, eliminate any arguments that currently satisfy their inputs
594-
let mut i = cmp:: min(arg_indexes.len(), input_indexes.len());
599+
let mut i = cmp::min(arg_indexes.len(), input_indexes.len());
595600
while i > 0 {
596601
let idx = i - 1;
597602
if compatibility_matrix[idx][idx] {
@@ -677,7 +682,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
677682
E0059, // FIXME: Choose a different code?
678683
"multiple arguments to this function are incorrect"
679684
);
680-
685+
681686
// Call out where the function is defined
682687
if let Some(def_id) = fn_def_id {
683688
if let Some(node) = tcx.hir().get_if_local(def_id) {
+9-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
enum E {
2-
X,
3-
Y
4-
}
5-
enum F {
6-
X2,
7-
Y2
8-
}
9-
struct G {
10-
}
11-
struct H {
12-
}
1+
// Some basic "obvious" cases for the heuristic error messages added for #65853
2+
3+
enum E { X, Y }
4+
enum F { X2, Y2 }
5+
struct G {}
6+
struct H {}
137
struct X {}
148
struct Y {}
159
struct Z {}
1610

1711

1812
fn invalid(_i: u32) {}
19-
2013
fn extra() {}
21-
2214
fn missing(_i: u32) {}
23-
2415
fn swapped(_i: u32, _s: &str) {}
25-
2616
fn permuted(_x: X, _y: Y, _z: Z) {}
27-
2817
fn complex(_i: u32, _s: &str, _e: E, _f: F, _g: G, _x: X, _y: Y, _z: Z ) {}
2918

3019
fn main() {
@@ -33,5 +22,6 @@ fn main() {
3322
missing(); //~ ERROR arguments to this function are incorrect
3423
swapped(&"", 1); //~ ERROR arguments to this function are incorrect
3524
permuted(Y {}, Z {}, X {}); //~ ERROR arguments to this function are incorrect
36-
complex(1.0, H {}, &"", G{}, F::X2, Z {}, X {}, Y {}); //~ ERROR arguments to this function are incorrect
37-
}
25+
complex(1.0, H {}, &"", G{}, F::X2, Z {}, X {}, Y {});
26+
//~^ ERROR arguments to this function are incorrect
27+
}

0 commit comments

Comments
 (0)