Skip to content

Commit

Permalink
Remove 1-tuple unreachable case
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrippling committed Jan 25, 2022
1 parent a129a85 commit a8bac98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
30 changes: 11 additions & 19 deletions compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use crate::structured_errors::StructuredDiagnostic;
use std::iter;
use std::slice;

enum FnArgsAsTuple<'hir> {
Single(&'hir hir::Expr<'hir>),
Multi { first: &'hir hir::Expr<'hir>, last: &'hir hir::Expr<'hir> },
struct FnArgsAsTuple<'hir> {
first: &'hir hir::Expr<'hir>,
last: &'hir hir::Expr<'hir>,
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -432,23 +432,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
String::from("()"),
Applicability::MachineApplicable,
);
} else if let Some(tuple_fn_arg) = sugg_tuple_wrap_args {
use FnArgsAsTuple::*;

let spans = match tuple_fn_arg {
Multi { first, last } => vec![
} else if let Some(FnArgsAsTuple { first, last }) = sugg_tuple_wrap_args {
err.multipart_suggestion(
"use parentheses to construct a tuple",
vec![
(first.span.shrink_to_lo(), '('.to_string()),
(last.span.shrink_to_hi(), ')'.to_string()),
],
Single(single) => vec![
(single.span.shrink_to_lo(), '('.to_string()),
(single.span.shrink_to_hi(), ",)".to_string()),
],
};

err.multipart_suggestion(
"use parentheses to construct a tuple",
spans,
Applicability::MachineApplicable,
);
} else {
Expand Down Expand Up @@ -519,8 +509,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if all_match {
match provided_args {
[] => None,
[single] => Some(FnArgsAsTuple::Single(single)),
[first, .., last] => Some(FnArgsAsTuple::Multi { first, last }),
[_] => unreachable!(
"shouldn't reach here - need count mismatch between 1-tuple and 1-argument"
),
[first, .., last] => Some(FnArgsAsTuple { first, last }),
}
} else {
None
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/suggestions/args-instead-of-tuple-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ fn main() {
//~^ ERROR this enum variant takes 1 argument but 2 arguments were supplied
int_bool(1, 2);
//~^ ERROR this function takes 1 argument but 2 arguments were supplied

let _: Option<(i8,)> = Some();
//~^ ERROR this enum variant takes 1 argument but 0 arguments were supplied
}

fn int_bool(_: (i32, bool)) {
Expand Down
12 changes: 10 additions & 2 deletions src/test/ui/suggestions/args-instead-of-tuple-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ LL | int_bool(1, 2);
| expected 1 argument
|
note: function defined here
--> $DIR/args-instead-of-tuple-errors.rs:12:4
--> $DIR/args-instead-of-tuple-errors.rs:15:4
|
LL | fn int_bool(_: (i32, bool)) {
| ^^^^^^^^ --------------

error: aborting due to 2 previous errors
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
--> $DIR/args-instead-of-tuple-errors.rs:11:28
|
LL | let _: Option<(i8,)> = Some();
| ^^^^-- supplied 0 arguments
| |
| expected 1 argument

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0061`.

0 comments on commit a8bac98

Please sign in to comment.