You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inference replaces bounded type parameter with sole implementer (was: action-at-a-distance regression) #18187
pubtraitScanCursor{fnpop_ws(&self) -> Self;}pubstructCursor;implScanCursorforCursor{fnpop_ws(&self) -> Cursor{unreachable!()}}pubtraitScanner{fnscan<Cur:ScanCursor>(cursor:&Cur) -> Self;}fnmain(){let cur :Cursor = Cursor;// let _tmp = &cur.pop_ws();// removing this still fails// let _ : Stmt = Scanner::scan(_tmp)let _ :Stmt = Scanner::scan(&cur.pop_ws());}structStmt;// Moving this above main compilesimplScannerforStmt{fnscan<Cur:ScanCursor>(cur:&Cur) -> Stmt{// Fails to compilelet _s:Stmt = Scanner::scan(&cur.pop_ws());loop{}}}// Added 2014-10-21#[cfg(compiles)]implScanCursorfor((),(()),((),(())))/* or whatever */{fnpop_ws(&self) -> ((),(()),((),(()))){unreachable!()}}
With an error in scan:
<anon>:29:38: 29:51 error: mismatched types: expected `&Cursor`, found `&Cur` (expected struct Cursor, found type parameter)
<anon>:29 let _s: Stmt = Scanner::scan(&cur.pop_ws());
^~~~~~~~~~~~~
error: aborting due to previous error
However, doing any of the 2 fixes suggested in the comments fixes this, which is strange, as the fixes don't touch the affected function.
The issue started at commit 590a61f788e058d7ae95806f55258bce3ae45567 (2014-10-13), which fixed issue #18019. The code compiles on older version of rustc.
@pfalabella encountered a similar issue on IRC, with the code:
use std::num::{One, one};#[cfg(not(test))]fnmain(){let _:Hamming<int> = Hamming::new(0);}/// representation of a Hamming number, allows to abstract on how the hamming number is storedpubtraitHammingNumber:Eq + Ord + Mul<Self,Self> + One{fnmultipliers() -> Self;}#[cfg(compiles)]implHammingNumberfori64{// returns the multipliers 2, 3 and 5 in the representation for the HammingNumberfnmultipliers() -> i64{0}}implHammingNumberforint{// returns the multipliers 2, 3 and 5 in the representation for the HammingNumberfnmultipliers() -> int{0}}pubstructHamming<T>{// Using a RingBuf as a queue, push to the back, pop from the frontq2:Vec<T>,}impl<T:HammingNumber>Hamming<T>{/// Static constructor method/// `n` initializes the capacity of the queuespubfnnew(n:uint) -> Hamming<T>{letmut h =
Hamming{q2:Vec::with_capacity(n),};
h.q2.push(one());
h
}/// Pushes the next multiple of `n` (x2, x3, x5) to the queuespubfnenqueue(&mutself,n:&T){let two :T = HammingNumber::multipliers();self.q2.push(*n * two);}}
Which gives the error
<anon>:44:23: 44:51 error: mismatched types: expected `T`, found `int` (expected type parameter, found int)
<anon>:44 let two : T = HammingNumber::multipliers();
But compiles with --cfg compiles (or if you remove the cfg test).
I added a similar #[cfg(compiles)] block to the original code.
The issue seems that when a trait Tr is impl-ed only by a single type A, and you have a function generic over T: Tr, then rustc replaces some of the occurrences of T in the function's body with A.
changed the title [-]Inference action-at-a-distance regression[/-][+]Inference replaces bounded type parameter with sole implementer (was: action-at-a-distance regression)[/+]on Oct 21, 2014
Activity
dwrensha commentedon Oct 20, 2014
Perhaps related to this other action-at-a-distance problem that I recently reported:
#18163
arielb1 commentedon Oct 21, 2014
@pfalabella encountered a similar issue on IRC, with the code:
Which gives the error
But compiles with
--cfg compiles(or if you remove the cfg test).arielb1 commentedon Oct 21, 2014
I added a similar
#[cfg(compiles)]block to the original code.The issue seems that when a trait
Trisimpl-ed only by a single typeA, and you have a function generic overT: Tr, then rustc replaces some of the occurrences ofTin the function's body withA.[-]Inference action-at-a-distance regression[/-][+]Inference replaces bounded type parameter with sole implementer (was: action-at-a-distance regression)[/+]nrc commentedon Oct 22, 2014
See #18209 for a much reduced test case
nikomatsakis commentedon Oct 22, 2014
Appears to be a dup of #18209, closing. (That is, my patch for #18209 fixes these test cases as well.)
Auto merge of rust-lang#18206 - ShoyuVanilla:issue-18187, r=Veykril