Repository: racer-rust/racer (language: rust)

Issue:
Racer 2.1.5 panics in searcher thread when trying to complete "fn m"
a.rs
'm' is arbitary.
```
fn m
```

when racer on it
```
$ RUST_BACKTRACE=full  racer complete 1 4 a.rs
PREFIX 3,4,m
thread 'searcher' panicked at 'byte index 18446744073709551615 is out of bounds of `fn m
`', libcore/str/mod.rs:2094:9
stack backtrace:
   0:     0x7f933157b6ee - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::hf6b91a10bc1f77b8
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1:     0x7f9331569256 - std::sys_common::backtrace::print::h4209434258458035
                               at libstd/sys_common/backtrace.rs:71
                               at libstd/sys_common/backtrace.rs:59
   2:     0x7f933156752d - std::panicking::default_hook::{{closure}}::h7fc4d356919adf96
                               at libstd/panicking.rs:211
   3:     0x7f93315672a0 - std::panicking::default_hook::h30d19ce6bd768405
                               at libstd/panicking.rs:227
   4:     0x7f9331567b9c - std::panicking::rust_panic_with_hook::hd04e46cfa0b30fca
                               at libstd/panicking.rs:477
   5:     0x7f9331567799 - std::panicking::continue_panic_fmt::ha2ede74723cdc31c
                               at libstd/panicking.rs:391
   6:     0x7f9331567695 - rust_begin_unwind
                               at libstd/panicking.rs:326
   7:     0x7f93315c471b - core::panicking::panic_fmt::hf3e826a5d8e962e1
                               at libcore/panicking.rs:77
   8:     0x7f93315bc35f - core::str::slice_error_fail::h88fd7006e1dcb1cd
                               at libcore/str/mod.rs:0
   9:     0x7f93312b4220 - core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::{{closure}}::habe4934aa864a8f5
                               at libcore/str/mod.rs:1894
  10:     0x7f9331294237 - racer::scopes::scope_start::h522de14faed0b362
                               at libcore/slice/mod.rs:0
  11:     0x7f9331294980 - racer::scopes::find_stmt_start::h97718d3ab2cf49a3
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/racer/scopes.rs:118
  12:     0x7f93313080a6 - racer::nameres::resolve_method::h3c9a980897e90b7c
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/racer/nameres.rs:1901
  13:     0x7f93312b7410 - racer::core::complete_from_file_::h06244d27b66b6c45
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/racer/core.rs:1043
  14:     0x7f9331263802 - racer::run_the_complete_fn::hf6d23cd652f3d20f
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/racer/core.rs:1000
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/bin/main.rs:159
  15:     0x7f933126aafe - std::sys_common::backtrace::__rust_begin_short_backtrace::h91bb7ddd73230154
                               at /home/hatoo/.cargo/registry/src/github.com-1ecc6299db9ec823/racer-2.1.5/src/bin/main.rs:88
                               at libstd/sys_common/backtrace.rs:136
  16:     0x7f9331259578 - std::panicking::try::do_call::hf757826e3bb6cb15
                               at libstd/thread/mod.rs:409
                               at libstd/panic.rs:313
                               at libstd/panicking.rs:310
  17:     0x7f933158d419 - __rust_maybe_catch_panic
                               at libpanic_unwind/lib.rs:102
  18:     0x7f9331252e9d - <F as alloc::boxed::FnBox<A>>::call_box::h70266601bba01e47
                               at libstd/panicking.rs:289
                               at libstd/panic.rs:392
                               at libstd/thread/mod.rs:408
                               at liballoc/boxed.rs:646
  19:     0x7f933157d84a - std::sys_common::thread::start_thread::h851ae18c961caf42
                               at liballoc/boxed.rs:656
                               at libstd/sys_common/thread.rs:24
  20:     0x7f9331568415 - std::sys::unix::thread::Thread::new::thread_start::hd5d0edde91f39da6
                               at libstd/sys/unix/thread.rs:90
  21:     0x7f93307c7493 - start_thread
  22:     0x7f93302e8ace - __clone
  23:                0x0 - <unknown>
ERROR 2018-09-17T06:05:03.611710900Z: racer: Search thread paniced: Any
END
```

18446744073709551615 seems  subtract overflow.


Discussion / hints:
Thanks!
Looks [this line](https://github.com/racer-rust/racer/blob/master/src/racer/nameres.rs#L1830) causes overflow :fearful: 
I'm going to resolve this after #957, but if you feel you want to fix it please open a PR :slightly_smiling_face: 

Fix patch:
diff --git a/src/racer/core.rs b/src/racer/core.rs
index a9d1297b..3a3f186f 100644
--- a/src/racer/core.rs
+++ b/src/racer/core.rs
@@ -154,6 +154,9 @@ impl BytePos {
     pub fn decrement(&self) -> Self {
         BytePos(self.0 - 1)
     }
+    pub fn try_decrement(&self) -> Option<Self> {
+        self.0.checked_sub(1).map(BytePos)
+    }
     /// returns self + 1
     pub fn increment(&self) -> Self {
         BytePos(self.0 + 1)
diff --git a/src/racer/nameres.rs b/src/racer/nameres.rs
index 9644e9ca..df529230 100644
--- a/src/racer/nameres.rs
+++ b/src/racer/nameres.rs
@@ -1827,7 +1827,12 @@ pub fn resolve_method(
         searchstr, point, scopestart,
     );
 
-    if let Some(stmtstart) = scopes::find_stmt_start(msrc, scopestart.decrement()) {
+    let parent_scope = match scopestart.try_decrement() {
+        Some(x) => x,
+        None => return vec![],
+    };
+
+    if let Some(stmtstart) = scopes::find_stmt_start(msrc, parent_scope) {
         let preblock = &msrc[stmtstart.0..scopestart.0];
         debug!("search_scope_headers preblock is |{}|", preblock);
 


Test patch: