Handle `GlobError::Build` in proc_macro error reporting

finchie
Jul 3, 2024, 8:06 AM
VYPJUPPKPVSIDCQPKZE2RJLMUQDI2IYV5COBQAEXI3VF3SF4CQTAC

Dependencies

  • [2] V5S5K33A Add basic error handling for invalid paths in proc_macro attribute
  • [3] SHNZZSZG Create `cli_macros` shim crate
  • [4] NO3PDO7P Refactor `fluent_embed` to support structs

Change contents

  • replacement in cli_macros/src/lib.rs at line 4
    [3.160][2.409:463](),[2.463][3.160:178](),[3.160][3.160:178]()
    use proc_macro_error::{emit_error, proc_macro_error};
    use quote::quote;
    [3.160]
    [2.464]
    use proc_macro_error::{abort, emit_error, proc_macro_error};
    use quote::{quote, ToTokens};
  • replacement in cli_macros/src/lib.rs at line 21
    [2.899][2.899:958]()
    GlobError::Build(_build_error) => todo!(),
    [2.899]
    [2.958]
    GlobError::Build(build_error) => {
    for location in build_error.locations() {
    // Create a token stream from the attribute's string literal
    let [proc_macro2::TokenTree::Literal(ref string_literal)] =
    derive_attribute
    .to_token_stream()
    .into_iter()
    .collect::<Vec<_>>()[..]
    else {
    abort!(derive_attribute, "unexpected macro attribute");
    };
    let (span_start, span_length) = location.span();
    let error_source = string_literal
    // Offset by 1 to skip the starting `"` double-quote character
    .subspan(span_start + 1..=span_start + span_length)
    // Fall back to the whole attribute if `subspan()` returns `None`
    // This will always happend on stable as subspan is nightly-only:
    // https://docs.rs/proc-macro2/latest/proc_macro2/struct.Literal.html#method.subspan
    .unwrap_or(derive_attribute.span());
    emit_error! { error_source, "invalid glob";
    note = location.to_string();
    };
    }
    }