Closed
Description
The following example try to compare two pointers:
fn do_cmp(a: &int, b: &int) {
io::println("do_cmp");
if a == b { io::println("same ref"); }
}
fn main() {
let a = @10;
let b = @10;
do_cmp(a, b);
do_cmp(a, a);
}
We have the following message:
try.rs:3:12: 3:13 error: mismatched types: expected &int
but found &int
(the anonymous lifetime #2 defined on the block at 1:28 does not necessarily outlive the anonymous lifetime #1 defined on the block at 1:28)
try.rs:3 if a == b { io::println("same ref"); }
^
error: aborting due to previous error
I think that it should compile. I think it should also compile if we have this prototype:
fn do_cmp(a: &int, b: @int)
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
catamorphism commentedon Aug 27, 2012
fn do_cmp(a: &int, b: @int)
This certainly shouldn't compile. You can't compare two things that have different types (if you really want to compare the two pointers, you can use
unsafe::reinterpret_cast
, but that will require you to use anunsafe
block).The first example looks like a bug, but @nikomatsakis should say for sure.
Vincent-Belliard commentedon Aug 27, 2012
I agree you can't compile with fn do_cmp(a: ~int, b: @int) because the two pointers will reference two regions of memory and the test will always be false.
However, we can pass a shared bowed pointer to a function which expect a borrowed pointer so we should be able to do the comparison. I didn't found in the documentation a way to convert explicitly a shared pointer to a borrowed pointer. For example, we should be able to do something like this:
let a = @10;
let b: &int = a;
or maybe:
let b: &int = a.convert_to_borrowed;
nikomatsakis commentedon Aug 27, 2012
The first example ought to work, I think, it's a bug in the type inferencer. Basically it should ensure that if you have an expression
a == b
then there is a common supertype T such thattype(a) <: T
andtype(b) <: T
, but I guess it's not doing that.nikomatsakis commentedon Aug 27, 2012
Regarding an explicit conversion to a borrowed pointer, the explicit way to write it for
@T
to&T
is&*a
(that is, take the address of the dereference ofa
). However, I think we will add a method into the standard library, so that you can write "a.borrow()". The compiler will automatically borrow for you in function calls (if the expected type of the parameter is&T
and you supply@T
).bblum commentedon Sep 4, 2012
Perhaps this should be supported inherently. In the meantime, we have
ptr::ref_eq
for this use case which takes two pointers with a fresh region variable on each one.nikomatsakis commentedon Sep 11, 2012
Reading the type check code, I see that there was a special bit of code for doing
==
comparisons which does not seem to be enforcing the correct requirement (that the two values being compared have a common supertype). I will fix that, it may fix this bug, though due to the limitations of our type inferencer it also may not.nielsegberts commentedon Feb 23, 2013
The example compiles, also with "b: @int" in the prototype. I think this bug can be closed?
nikomatsakis commentedon Feb 24, 2013
Agreed.
Apply rustfmt::skip on imports (rust-lang#3289)
Stop recommending cargo clean in README
Auto merge of rust-lang#3290 - tv42:master, r=saethlin