rust-embedded / rust-embedded/svd2rust

thread 'main' panicked at src/util.rs:95:9: "...%s" is not a valid Ident

Open
#811 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
857
Forks
164
PR merge metrics
No merged PRs in 30d

Description

I'm working on patching and fixing up the SVD for the LPC11U1x/2x/3x series of MCUs. One of the modifications I made was to use derivedFrom in more places (so that the types used could also be used everywhere), and another was to add a generic set of IOCON registers. Part of the SVD in question

<field derivedFrom="IOCON.PIO0[%s].MODE">, however, causes svd2rust to panic with pio0%s is not a valid Ident. I attempted to resolve this on master by doing a patch similar to the following

diff --git a/src/generate/register.rs b/src/generate/register.rs
index 21fb174..d0fe56b 100644
--- a/src/generate/register.rs
+++ b/src/generate/register.rs
@@ -1462,7 +1462,7 @@ fn base_syn_path(
     } else if base.register().block == fpath.register().block {
         let mut segments = Punctuated::new();
         segments.push(path_segment(Ident::new("super", span)));
-        segments.push(path_segment(base.register().name.to_snake_case_ident(span)));
+        segments.push(path_segment(replace_suffix(&base.register().name, "").to_snake_case_ident(span)));
         segments.push(path_segment(base_ident.clone()));
         type_path(segments)
     } else {

This leads to svd2rust not panicking, though it does produce invalid code for the IOCON RegisterBlock (return type of PIO10 instead of PIO1 for the pio1 method, &PIO10 instead of &PIO1 for pio1_iter). I did a similar patch to case-defaults (#805), and this resulted in what appeared to be correct codegen.

RUST_BACKTRACE=1 panic:

thread 'main' panicked at src/util.rs:95:9:
"pio0%s" is not a valid Ident
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f688dd684faca5b31b156fac2c6e0ae81fc9bc90/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/f688dd684faca5b31b156fac2c6e0ae81fc9bc90/library/core/src/panicking.rs:72:14
   2: proc_macro2::fallback::validate_ident
             at /home/masterr3c0rd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.71/src/fallback.rs:828:9
   3: proc_macro2::fallback::Ident::new_checked
             at /home/masterr3c0rd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.71/src/fallback.rs:760:9
   4: proc_macro2::imp::Ident::new_checked
             at /home/masterr3c0rd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.71/src/wrapper.rs:646:50
   5: proc_macro2::Ident::new
             at /home/masterr3c0rd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.71/src/lib.rs:956:21
   6: svd2rust::util::ToSanitizedCase::to_snake_case_ident
             at /mnt/projects/Personal/svd2rust/src/util.rs:95:9
   7: svd2rust::generate::register::base_syn_path
             at /mnt/projects/Personal/svd2rust/src/generate/register.rs:1465:36
   8: svd2rust::generate::register::fields
             at /mnt/projects/Personal/svd2rust/src/generate/register.rs:883:37
   9: svd2rust::generate::register::render_register_mod
             at /mnt/projects/Personal/svd2rust/src/generate/register.rs:283:17
  10: svd2rust::generate::register::render
             at /mnt/projects/Personal/svd2rust/src/generate/register.rs:112:25
  11: svd2rust::generate::peripheral::render_ercs
             at /mnt/projects/Personal/svd2rust/src/generate/peripheral.rs:1355:36
  12: svd2rust::generate::peripheral::render
             at /mnt/projects/Personal/svd2rust/src/generate/peripheral.rs:240:21
  13: svd2rust::generate::device::render
             at /mnt/projects/Personal/svd2rust/src/generate/device.rs:204:22
  14: svd2rust::run
             at /mnt/projects/Personal/svd2rust/src/main.rs:272:17
  15: svd2rust::main
             at /mnt/projects/Personal/svd2rust/src/main.rs:363:25
  16: core::ops::function::FnOnce::call_once
             at /rustc/f688dd684faca5b31b156fac2c6e0ae81fc9bc90/library/core/src/ops/function.rs:250:5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/generate/register.rs:1462 and src/util.rs:95, then reproduce the panic using the LPC11U1x/2x/3x SVD fragment linked in the issue. Trace how derivedFrom names containing %s become identifiers and inspect the related case-defaults change in #805. Done means generation no longer panics and produces correct IOCON RegisterBlock references for pio1 and pio1_iter.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.