Skip to content

Do not emit help when shorthand from macro when suggest ? or expect #141070

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

let sugg = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) {
Some(_) if expr.span.from_expansion() => return false,
Some(ident) => format!(": {ident}{sugg}"),
None => sugg.to_string(),
};
Expand Down
56 changes: 56 additions & 0 deletions tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
trait Reencode {
type Error;
fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error>;
}

struct Reencoder;
impl Reencode for Reencoder {
type Error = &'static str;
fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error> {
Ok(tag)
}
}


enum Operator {
Suspend { tag_index: u32 },
}

enum Instruction {
Suspend { tag_index: u32 },
}


macro_rules! for_each_operator {
($m:ident) => {
$m! {
Suspend { tag_index: u32 } => visit_suspend
}
};
}


fn process<T: Reencode>(op: &Operator, reencoder: &mut T) -> Instruction {
macro_rules! translate {
(Suspend { tag_index: $ty:ty } => $visit:ident) => {
match op {
Operator::Suspend { tag_index } => {
let tag_index = reencoder.tag_index(*tag_index);

// KEY POINT: Using field shorthand syntax where the compiler gets confused
// Here tag_index is a Result<u32, E> but we're using it where u32 is expected
Instruction::Suspend { tag_index } //~ ERROR mismatched types [E0308]
}
}
};
}

for_each_operator!(translate)
Comment on lines +47 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I delete the unused comment.

}

fn main() {
let mut reencoder = Reencoder;
let op = Operator::Suspend { tag_index: 1 };

let _ = process(&op, &mut reencoder);
}
16 changes: 16 additions & 0 deletions tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/macro-shorthand-issue-140659.rs:42:44
|
LL | Instruction::Suspend { tag_index }
| ^^^^^^^^^ expected `u32`, found `Result<u32, <T as Reencode>::Error>`
...
LL | for_each_operator!(translate)
| ----------------------------- in this macro invocation
|
= note: expected type `u32`
found enum `Result<u32, <T as Reencode>::Error>`
= note: this error originates in the macro `translate` which comes from the expansion of the macro `for_each_operator` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I create tests/ui/typeck/suggestions/ amd move some testcases to it.

File renamed without changes.
Loading