From 60a658250ea2ee6dd10425d7070c373262762b1c Mon Sep 17 00:00:00 2001
From: Lindsey Kuper <lindsey@rockstargirl.org>
Date: Wed, 20 Jun 2012 14:33:25 -0700
Subject: [PATCH] Don't force resolution of integral type vars in unary minus
 exprs

These were getting resolved too early, when they were still
unconstrained by the rest of the typing context.  Waiting a bit longer
to resolve them gives the rest of the typing context a chance to come
into play, so that they don't default to `int`.
---
 src/rustc/middle/typeck/check.rs                      | 9 ++++++++-
 src/test/run-pass/integer-literal-suffix-inference.rs | 8 ++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/rustc/middle/typeck/check.rs b/src/rustc/middle/typeck/check.rs
index 04d3c4f90c5..b126e5c55ff 100644
--- a/src/rustc/middle/typeck/check.rs
+++ b/src/rustc/middle/typeck/check.rs
@@ -1198,7 +1198,14 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
             }
           }
           ast::neg {
-            oprnd_t = structurally_resolved_type(fcx, oprnd.span, oprnd_t);
+            // If the operand's type is an integral type variable, we
+            // don't want to resolve it yet, because the rest of the
+            // typing context might not have had the opportunity to
+            // constrain it yet.
+            if !(ty::type_is_var_integral(oprnd_t)) {
+                oprnd_t = structurally_resolved_type(fcx, oprnd.span,
+                                                     oprnd_t);
+            }
             if !(ty::type_is_integral(oprnd_t) ||
                  ty::type_is_fp(oprnd_t)) {
                 oprnd_t = check_user_unop(fcx, "-", "unary-", expr,
diff --git a/src/test/run-pass/integer-literal-suffix-inference.rs b/src/test/run-pass/integer-literal-suffix-inference.rs
index 8fd51180477..5d157658aa1 100644
--- a/src/test/run-pass/integer-literal-suffix-inference.rs
+++ b/src/test/run-pass/integer-literal-suffix-inference.rs
@@ -16,22 +16,22 @@ fn main() {
 
     let _i: i8 = -128;
     let j = -128;
-    // id_i8(j);
+    id_i8(j);
     id_i8(-128);
 
     let _i: i16 = -32_768;
     let j = -32_768;
-    // id_i16(j);
+    id_i16(j);
     id_i16(-32_768);
 
     let _i: i32 = -2_147_483_648;
     let j = -2_147_483_648;
-    // id_i32(j);
+    id_i32(j);
     id_i32(-2_147_483_648);
 
     let _i: i64 = -9_223_372_036_854_775_808;
     let j = -9_223_372_036_854_775_808;
-    // id_i64(j);
+    id_i64(j);
     id_i64(-9_223_372_036_854_775_808);
 
     let _i: uint = 1;