Skip to content

borrowed pointers should be compared #3289

Closed
@Vincent-Belliard

Description

@Vincent-Belliard
Contributor

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)

Activity

catamorphism

catamorphism commented on Aug 27, 2012

@catamorphism
Contributor

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 an unsafe block).

The first example looks like a bug, but @nikomatsakis should say for sure.

Vincent-Belliard

Vincent-Belliard commented on Aug 27, 2012

@Vincent-Belliard
ContributorAuthor

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

nikomatsakis commented on Aug 27, 2012

@nikomatsakis
Contributor

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 that type(a) <: T and type(b) <: T, but I guess it's not doing that.

nikomatsakis

nikomatsakis commented on Aug 27, 2012

@nikomatsakis
Contributor

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 of a). 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

bblum commented on Sep 4, 2012

@bblum
Contributor

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

nikomatsakis commented on Sep 11, 2012

@nikomatsakis
Contributor

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

nielsegberts commented on Feb 23, 2013

@nielsegberts
Contributor

The example compiles, also with "b: @int" in the prototype. I think this bug can be closed?

nikomatsakis

nikomatsakis commented on Feb 24, 2013

@nikomatsakis
Contributor

Agreed.

added a commit that references this issue on May 15, 2021
added 2 commits that reference this issue on Feb 17, 2024
5587a37

Auto merge of rust-lang#3290 - tv42:master, r=saethlin

f45a5b1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regions

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @nikomatsakis@nielsegberts@catamorphism@bblum@Vincent-Belliard

      Issue actions

        borrowed pointers should be compared · Issue #3289 · rust-lang/rust