Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HRTBs for function traits #601

Merged
merged 9 commits into from
Aug 18, 2023
Merged

Use HRTBs for function traits #601

merged 9 commits into from
Aug 18, 2023

Conversation

udoprog
Copy link
Collaborator

@udoprog udoprog commented Aug 18, 2023

This is necessary to avoid unsound use of references in registered functions. An issue that was discovered when investigating #600.

The way function arguments are currently modeled uses unbound references, which among other things allow you to register a 'static reference or any associated lifetime allowing values to escape:

#[rune::function]
fn download(url: &'static str) {
    todo!()
}

struct Foo<'a> {
    url: &'a str,
}

impl<'a> Foo<'a> {
    fn download(url: &'a str) {
        self.url = url;
    }
}

let module = Module::new();
module.function_meta(download);

With this change, functions like the above are correctly rejected instead. A downside is that we can no longer use references for arguments in async functions. Instead these must use owned types:

#[rune::function]
async fn download(url: &str) {
}

Becomes:

#[rune::function]
async fn download(url: Ref<str>) {
}

And associated types like this:

struct Foo;

impl Foo {
    #[rune::function]
    async fn download(&self, url: &str) {
    }
}

Becomes:

struct Foo;

impl Foo {
    #[rune::function(instance, path = Self::download)]
    async fn download(this: Ref<Self>, url: Ref<str>) {
    }
}

@udoprog udoprog added the bug Something isn't working label Aug 18, 2023
@udoprog udoprog merged commit 0e0bb0a into main Aug 18, 2023
16 checks passed
@udoprog udoprog deleted the hrtb-function-trait branch August 18, 2023 03:36
@udoprog udoprog added soundness Issues related to potential outstanding soundness holes. changelog Issue has been added to the changelog labels Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working changelog Issue has been added to the changelog soundness Issues related to potential outstanding soundness holes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant