Skip to content
New issue

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

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

Already on GitHub? # to your account

Allow lifetimes in function pointer return values #1847

Merged
merged 1 commit into from
Feb 2, 2025
Merged

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Feb 2, 2025

Example:

use syn::parse::{ParseBuffer, ParseStream, Result};
use syn::token::{Brace, Bracket, Paren};
use syn::{braced, bracketed, parenthesized, MacroDelimiter};

fn parse(input: ParseStream) -> Result<()> {
    let (_delim, _content) = input.call(macro_delimiter)?;
    Ok(())
}

fn macro_delimiter<'a>(input: ParseStream<'a>) -> Result<(MacroDelimiter, ParseBuffer<'a>)> {
    let content;
    let lookahead = input.lookahead1();
    let delim = if input.peek(Paren) {
        MacroDelimiter::Paren(parenthesized!(content in input))
    } else if input.peek(Brace) {
        MacroDelimiter::Brace(braced!(content in input))
    } else if input.peek(Bracket) {
        MacroDelimiter::Bracket(bracketed!(content in input))
    } else {
        return Err(lookahead.error());
    };
    Ok((delim, content))
}

Before:

error[E0308]: mismatched types
   --> src/main.rs:6:41
    |
6   |     let (_delim, _content) = input.call(macro_delimiter)?;
    |                                    ---- ^^^^^^^^^^^^^^^ one type is more general than the other
    |                                    |
    |                                    arguments to this method are incorrect
    |
    = note: expected fn pointer `for<'a> fn(&'a ParseBuffer<'a>) -> Result<_, _>`
                  found fn item `for<'a> fn(&'a ParseBuffer<'a>) -> Result<(MacroDelimiter, ParseBuffer<'a>), _> {macro_delimiter}`
note: method defined here
   --> syn/src/parse.rs:505:12
    |
505 |     pub fn call<T>(&self, function: fn(ParseStream) -> Result<T>) -> Result<T> {
    |            ^^^^

After: works.

@dtolnay dtolnay merged commit f0c57ee into master Feb 2, 2025
30 checks passed
@dtolnay dtolnay deleted the lifetimes branch February 2, 2025 01:04
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant