Skip to content

"unsafe" on methods doesn't work #3080

Closed

Description

@bblum

This compiles, and shouldn't:

enum x = ();
impl x {
    unsafe fn with() { }
}

fn main() {
    x(()).with();
}

Activity

  1. eholk commented on Aug 1, 2012

    @eholk
    Contributor

    This might be related to #2587.

  2. vertexclique commented on Oct 23, 2012

    @vertexclique
    Contributor

    Latest head solved this with throwing error case

  3. eholk commented on Nov 26, 2012

    @eholk
    Contributor

    I just tried compiling this program with the latest incoming, and it still compiled successfully. @vertexclique, did you say you got an error with this test program?

  4. catamorphism commented on Dec 6, 2012

    @catamorphism
    Contributor

    Still wrong as of b60a0be (no error message)

  5. kud1ing commented on Feb 6, 2013

    @kud1ing

    Can this be closed? I have a working example of unsafe fn foo() methods.

  6. pnkfelix commented on Mar 22, 2013

    @pnkfelix
    Contributor

    Indeed, I am wondering the same thing as kud1ing.

    I quickly hacked up an example program that uses unsafe features, and it seems to compile and run properly. (I've included the example at the end of this comment.)

    I do not know enough about how traits and methods are implemented, especially in the case of dynamic dispatch in object types, to say that we currently support unsafe methods; all I can see is that we do not obviously fail to support them. :) So I won't close this ticket immediately, but I'd like someone else on the team to consider doing so (or provide me with a pointer to something saying why we do not and/or can not support unsafe methods).


    unsafe fn f(x:*int) -> int {
        let p = x as int;
        let q = p + 8; // danger: assumes sizeof(int*) in a vec is 8 !
        let y = q as *int;
        *y
    }
    
    trait x { unsafe fn uwith(&self, x:*int) -> int; }
    impl x for () {
        unsafe fn uwith(&self, x:*int) -> int {  3+f(x) }
    }
    
    impl x for @() {
        unsafe fn uwith(&self, x:*int) -> int { 13+f(x) }
    }
    
    impl x for @int {
        unsafe fn uwith(&self, x:*int) -> int { 23+f(x) }
    }
    
    fn main() {
        let obju = ();
        let objo = @();
        let obji = @3;
        let vec = [0,7,1000];
        io::print(fmt!("%?\n", obju.uwith(&vec[0])));
        io::print(fmt!("%?\n", objo.uwith(&vec[0])));
        io::print(fmt!("%?\n", obji.uwith(&vec[0])));
    }
  7. pnkfelix commented on Mar 22, 2013

    @pnkfelix
    Contributor

    (either way, this is not a 0.6 milestone issue.)

  8. pnkfelix commented on Mar 25, 2013

    @pnkfelix
    Contributor

    In case it is not clear: I was able to compile and run the code from the comment above. I had meant for this to demonstrate that unsafe methods might be implemented and might work; Niko separately pointed out that the code in the comment also demonstrates that there is a bug, since the calls to the unsafe uwith method should have been required to be enclosed in an unsafe block within their invocations inside main.

  9. nikomatsakis commented on May 8, 2013

    @nikomatsakis
    Contributor

    More examples in #6331

  10. brson commented on Jul 19, 2013

    @brson
    Contributor

    This is fixed.

  11. added a commit that references this issue on May 15, 2021
  12. added a commit that references this issue on Aug 21, 2026
  13. added a commit that references this issue on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions