Rollup merge of #110096 - compiler-errors:tweak-tuple-idx-msg, r=Nilstrieb
Tweak tuple indexing suggestion Fixes #110091
This commit is contained in:
commit
eed27ac7f4
5 changed files with 35 additions and 17 deletions
|
@ -2810,23 +2810,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
"cannot index into a value of type `{base_t}`",
|
"cannot index into a value of type `{base_t}`",
|
||||||
);
|
);
|
||||||
// Try to give some advice about indexing tuples.
|
// Try to give some advice about indexing tuples.
|
||||||
if let ty::Tuple(..) = base_t.kind() {
|
if let ty::Tuple(types) = base_t.kind() {
|
||||||
let mut needs_note = true;
|
let mut needs_note = true;
|
||||||
// If the index is an integer, we can show the actual
|
// If the index is an integer, we can show the actual
|
||||||
// fixed expression:
|
// fixed expression:
|
||||||
if let ExprKind::Lit(ref lit) = idx.kind {
|
if let ExprKind::Lit(ref lit) = idx.kind
|
||||||
if let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node {
|
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
|
||||||
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
|
&& i < types.len().try_into().expect("expected tuple index to be < usize length")
|
||||||
if let Ok(snip) = snip {
|
{
|
||||||
err.span_suggestion(
|
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
|
||||||
expr.span,
|
if let Ok(snip) = snip {
|
||||||
"to access tuple elements, use",
|
err.span_suggestion(
|
||||||
format!("{snip}.{i}"),
|
expr.span,
|
||||||
Applicability::MachineApplicable,
|
"to access tuple elements, use",
|
||||||
);
|
format!("{snip}.{i}"),
|
||||||
needs_note = false;
|
Applicability::MachineApplicable,
|
||||||
}
|
);
|
||||||
|
needs_note = false;
|
||||||
}
|
}
|
||||||
|
} else if let ExprKind::Path(..) = idx.peel_borrows().kind {
|
||||||
|
err.span_label(idx.span, "cannot access tuple elements at a variable index");
|
||||||
}
|
}
|
||||||
if needs_note {
|
if needs_note {
|
||||||
err.help(
|
err.help(
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let z = ();
|
let z = (10,);
|
||||||
let _ = z[0]; //~ ERROR cannot index into a value of type `()`
|
let _ = z[0]; //~ ERROR cannot index into a value of type `({integer},)`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
error[E0608]: cannot index into a value of type `()`
|
error[E0608]: cannot index into a value of type `({integer},)`
|
||||||
--> $DIR/index_message.rs:3:13
|
--> $DIR/index_message.rs:3:13
|
||||||
|
|
|
|
||||||
LL | let _ = z[0];
|
LL | let _ = z[0];
|
||||||
|
|
|
@ -8,4 +8,9 @@ fn main() {
|
||||||
let i = 0_usize;
|
let i = 0_usize;
|
||||||
let _ = tup[i];
|
let _ = tup[i];
|
||||||
//~^ ERROR cannot index into a value of type
|
//~^ ERROR cannot index into a value of type
|
||||||
|
|
||||||
|
// the case where the index is out of bounds
|
||||||
|
let tup = (10,);
|
||||||
|
let _ = tup[3];
|
||||||
|
//~^ ERROR cannot index into a value of type
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,10 +8,20 @@ error[E0608]: cannot index into a value of type `({integer}, {integer}, {integer
|
||||||
--> $DIR/issue-27842.rs:9:13
|
--> $DIR/issue-27842.rs:9:13
|
||||||
|
|
|
|
||||||
LL | let _ = tup[i];
|
LL | let _ = tup[i];
|
||||||
|
| ^^^^-^
|
||||||
|
| |
|
||||||
|
| cannot access tuple elements at a variable index
|
||||||
|
|
|
||||||
|
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
|
||||||
|
|
||||||
|
error[E0608]: cannot index into a value of type `({integer},)`
|
||||||
|
--> $DIR/issue-27842.rs:14:13
|
||||||
|
|
|
||||||
|
LL | let _ = tup[3];
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|
|
|
||||||
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
|
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0608`.
|
For more information about this error, try `rustc --explain E0608`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue