1
1
use crate :: check:: method:: MethodCallee ;
2
2
use crate :: check:: { has_expected_num_generic_args, FnCtxt , PlaceOp } ;
3
+ use rustc_ast as ast;
4
+ use rustc_errors:: Applicability ;
3
5
use rustc_hir as hir;
4
6
use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
5
7
use rustc_infer:: infer:: InferOk ;
@@ -47,6 +49,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
47
49
expr : & hir:: Expr < ' _ > ,
48
50
base_expr : & ' tcx hir:: Expr < ' tcx > ,
49
51
base_ty : Ty < ' tcx > ,
52
+ index_expr : & ' tcx hir:: Expr < ' tcx > ,
50
53
idx_ty : Ty < ' tcx > ,
51
54
) -> Option < ( /*index type*/ Ty < ' tcx > , /*element type*/ Ty < ' tcx > ) > {
52
55
// FIXME(#18741) -- this is almost but not quite the same as the
@@ -56,12 +59,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
56
59
let mut autoderef = self . autoderef ( base_expr. span , base_ty) ;
57
60
let mut result = None ;
58
61
while result. is_none ( ) && autoderef. next ( ) . is_some ( ) {
59
- result = self . try_index_step ( expr, base_expr, & autoderef, idx_ty) ;
62
+ result = self . try_index_step ( expr, base_expr, & autoderef, idx_ty, index_expr ) ;
60
63
}
61
64
self . register_predicates ( autoderef. into_obligations ( ) ) ;
62
65
result
63
66
}
64
67
68
+ fn negative_index (
69
+ & self ,
70
+ ty : Ty < ' tcx > ,
71
+ span : Span ,
72
+ base_expr : & hir:: Expr < ' _ > ,
73
+ ) -> Option < ( Ty < ' tcx > , Ty < ' tcx > ) > {
74
+ let ty = self . resolve_vars_if_possible ( ty) ;
75
+ let mut err = self . tcx . sess . struct_span_err (
76
+ span,
77
+ & format ! ( "negative integers cannot be used to index on a `{}`" , ty) ,
78
+ ) ;
79
+ err. span_label ( span, & format ! ( "cannot use a negative integer for indexing on `{}`" , ty) ) ;
80
+ if let ( hir:: ExprKind :: Path ( ..) , Ok ( snippet) ) =
81
+ ( & base_expr. kind , self . tcx . sess . source_map ( ) . span_to_snippet ( base_expr. span ) )
82
+ {
83
+ // `foo[-1]` to `foo[foo.len() - 1]`
84
+ err. span_suggestion_verbose (
85
+ span. shrink_to_lo ( ) ,
86
+ & format ! (
87
+ "to access an element starting from the end of the `{}`, compute the index" ,
88
+ ty,
89
+ ) ,
90
+ format ! ( "{}.len() " , snippet) ,
91
+ Applicability :: MachineApplicable ,
92
+ ) ;
93
+ }
94
+ err. emit ( ) ;
95
+ Some ( ( self . tcx . ty_error ( ) , self . tcx . ty_error ( ) ) )
96
+ }
97
+
65
98
/// To type-check `base_expr[index_expr]`, we progressively autoderef
66
99
/// (and otherwise adjust) `base_expr`, looking for a type which either
67
100
/// supports builtin indexing or overloaded indexing.
@@ -73,6 +106,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
73
106
base_expr : & hir:: Expr < ' _ > ,
74
107
autoderef : & Autoderef < ' a , ' tcx > ,
75
108
index_ty : Ty < ' tcx > ,
109
+ index_expr : & hir:: Expr < ' _ > ,
76
110
) -> Option < ( /*index type*/ Ty < ' tcx > , /*element type*/ Ty < ' tcx > ) > {
77
111
let adjusted_ty =
78
112
self . structurally_resolved_type ( autoderef. span ( ) , autoderef. final_ty ( false ) ) ;
@@ -82,6 +116,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
82
116
expr, base_expr, adjusted_ty, index_ty
83
117
) ;
84
118
119
+ if let hir:: ExprKind :: Unary (
120
+ hir:: UnOp :: Neg ,
121
+ hir:: Expr {
122
+ kind : hir:: ExprKind :: Lit ( hir:: Lit { node : ast:: LitKind :: Int ( ..) , .. } ) ,
123
+ ..
124
+ } ,
125
+ ) = index_expr. kind
126
+ {
127
+ match adjusted_ty. kind ( ) {
128
+ ty:: Adt ( ty:: AdtDef { did, .. } , _)
129
+ if self . tcx . is_diagnostic_item ( sym:: vec_type, * did) =>
130
+ {
131
+ return self . negative_index ( adjusted_ty, index_expr. span , base_expr) ;
132
+ }
133
+ ty:: Slice ( _) | ty:: Array ( _, _) => {
134
+ return self . negative_index ( adjusted_ty, index_expr. span , base_expr) ;
135
+ }
136
+ _ => { }
137
+ }
138
+ }
139
+
85
140
for unsize in [ false , true ] {
86
141
let mut self_ty = adjusted_ty;
87
142
if unsize {
0 commit comments