Conversation

Jarkko Sakkinen

Edited 2 days ago
I can do this:

rustc --crate-type lib --crate-name tpm2_protocol tpm2_protocol/src/lib.rs --edition=2021 --emit=mir

Cool. Can I compile a single file from a crate project? This would be great when doing large refactor and not get your terminal DOS'd :-)

It's pain really. ATM, I'm refactoring out a macro called "tpm_response!" (it is replaced with pre-existing "tpm_struct!" that is extended) and I need to do similar changes to few dozen files.

I'd like to:

1. Do the edit to a single file.
2. Try if that file compiles.
3. Move on to next files.

Lack of knowledge with this makes refactors in Rust living hell TBH ...

Some constrains in my project that might help (possibly):

1. no_std
2. no deps
3. no alloc

Another way to express this: what is "gcc -c" of rustc?

#rust
3
1
1

@jarkko The compilation unit in Rust is the crate. So you generally can't compile a single file.

Also, as a tip, don't invoke rustc directly unless you really really have to for something specific. Use cargo for everything.

Rustc doesn't know about project structure or dependencies.

I've been using Rust full time since 2019 and I think I've called rustc directly less than 10 times

1
0
0

@jarkko As for how not to get DOS'ed, depends on the situation. Typically I just look at the bottom error, fix it, and compile it again.

But there are ways to decrease the amount of errors using e.g. the `todo!` macro (which returns the !/never type which can be coerced to any type and thus won't emit a type error)

1
0
0

@jarkko don't know if this covers what you are looking for, but looks like the most likely candidate.

https://www.reddit.com/r/rust/comments/tiaor0/comment/i1ft83t

1
0
0

@jarkko It doesn't directly address your question, but to combat terminal DOS I use "bacon check", which is a bit like "watch cargo" except it re-runs Cargo if it sees files change, so you can pick away at the errors one by one https://github.com/Canop/bacon

1
0
1

@diondokter @jarkko additionally, if the number of unnecessary errors is too high because they are implied by earlier ones, that's considered a bug: rustc is meant to silence the latter ones so only relevant ones are displayed. If it's not doing that, having a ticket with a repro would be helpful.

1
0
1
@Netux yeah, so would hard to apply to the context let's put it that way :-)
1
0
1
@ekuber @diondokter can you make it stop on the first error catched? that would pretty much sort my issues.
1
0
0
@TalesFromTheArmchair nope but it looks interesting, thanks :-)
0
0
0

@jarkko @diondokter the closest to that that the compiler has is the nightly only -Ztreat-err-as-bug. The compiler used to stop after encountering errors in a given stage, and the user feedback on doing that was quite negative.
Is piping to a pager like less not a possibility for you?

1
0
0
@ekuber @diondokter of course i use less as workaround for crappy compilation flow or actually head -100 or similar
1
0
0
@ekuber @diondokter but yeah it's a workaround i manage to do refactorizations about 10x slower than with C and more error prone with the same factor :-) quite bad tho
1
0
0
@diondokter @ekuber I did some reseach and unfortunately rustc works like that :-/

well what can you do...
1
0
0
@diondokter @ekuber I absolutely hate this so much but I'll survive :-) https://github.com/puavo-org/tpm2_library/commit/f29d1a536af50ae55412ba36a23f957e3b1e0561

It does the job at least.
1
0
0
@diondokter @ekuber

~/work/github.com/jarkkojs/tpm2_library main*
❯ scripts/rustc-error.py tpm2_protocol tpm2_protocol/src/lib.rs message/mod.rs

error[E0412]: cannot find type `TpmNvUndefineSpaceSpecialCommand` in this scope
--> tpm2_protocol/src/message/mod.rs:65:6
|
65 | (TpmNvUndefineSpaceSpecialCommand, TpmNvUndefineSpaceSpecialResponse, NvUndefineSpaceSpecial),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: a struct with a similar name exists: `TpmNvUndefineSpaceSpecialResponse`
|
::: tpm2_protocol/src/macro/struct.rs:93:9
|
93 | pub struct $name {
| ---------------- similarly named struct `TpmNvUndefineSpaceSpecialResponse` defined here
|
::: tpm2_protocol/src/macro/mod.rs:174:25
|
174 | $( $variant($cmd), )*
| ---- due to this macro variable
1
0
0
@diondokter @ekuber but that's life and everything has disadvantages but this being a "Microsoft bug" does not make it better.
1
0
0
@diondokter @ekuber BUT on bright sound: I have pattern to solve output issues i.e., json output :-) it's easier than capsulating a file to a temp crate at least (and does not contaminate compilation results).
1
0
0

Jarkko Sakkinen

Edited yesterday
@diondokter @ekuber Sorry I was just super frustrated :-) On plus side, I can refine that Python script over time and perhaps get something useful out of json output. And thanks for all thet tips! It's the part I don't like in this compiler but nothing is perfect 🤷
0
0
1
@Netux IMHO my python script was much better workaround :-) JSON output is the thing with rustc because otherwise data is unmanageable... But from that you cherry pick the stuff you care.
1
0
1
@Netux I can share the context that led me ripping my hairs of:

https://github.com/puavo-org/tpm2_library/commit/0e79aff5ed4ea52c553b97e74f2bdbb4819ffb84

This was due misconducted major refactoring, and type of refactoring that you cannot simply use some "LSP do the magic" button but have to conduct with ultimate care. I got mixed up in the middle because I was not anymore sure of errors that are just due incomplete changes, and those that are bugs caused by me during refactorization process. I finished it anyway because I was "too far to head back" ;-)

It took me two days and two sleepless nights to conduct that fix (these are the worst wher you have dozen of scattered 1-2 line tweaks here and there). The Python script I did, despite archaic help definitely keep my head together with these. I've also like "sacrified my life" for three weeks to write down this 7000 line protocol crate, which probably also makes me a bit more "trigger frenzy" than the usual grumpy me ;-)
1
1
1

@jarkko oh that is some fiddly code. Rust is weird, I thought it was just updated objectiveC, apparently it was written on OCaml. That explains some of the weird.

Let instead of assignment with = just seems like some user hostility built into the language. I expect it's for things that are managed instead of just raw first order data types.

Definitely can see why the LSP route wasn't a viable choice.

0
0
0