Skip to content

Commit

Permalink
Rollup merge of rust-lang#137415 - chenyukang:yukang-fix-137345-inval…
Browse files Browse the repository at this point in the history
…id-sugg, r=estebank

Remove invalid suggestion of into_iter for extern macro

Fixes rust-lang#137345

rust-lang#109082 is closed due to performance issue, do we have any other solution for this kind of issue?
  • Loading branch information
matthiaskrgr authored Feb 22, 2025
2 parents 88ed69c + c45463e commit e780b89
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
12 changes: 7 additions & 5 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,11 +905,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if self.impl_into_iterator_should_be_iterator(rcvr_ty, span, unsatisfied_predicates)
{
err.span_label(span, format!("`{rcvr_ty}` is not an iterator"));
err.multipart_suggestion_verbose(
"call `.into_iter()` first",
vec![(span.shrink_to_lo(), format!("into_iter()."))],
Applicability::MaybeIncorrect,
);
if !span.in_external_macro(self.tcx.sess.source_map()) {
err.multipart_suggestion_verbose(
"call `.into_iter()` first",
vec![(span.shrink_to_lo(), format!("into_iter()."))],
Applicability::MaybeIncorrect,
);
}
return err.emit();
} else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
// We special case the situation where we are looking for `_` in
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/proc-macro/auxiliary/quote-issue-137345.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate proc_macro;
use proc_macro::TokenStream;

fn items() -> impl IntoIterator<Item = i32> {
vec![1, 2, 3]
}

#[macro_export]
macro_rules! quote {
// Rule for any other number of tokens.
($($tt:tt)*) => {{
fn items() -> impl IntoIterator<Item = i32> {
vec![1, 2, 3]
}
let _s = TokenStream::new();
let other_items = items().map(|i| i + 1);
_s
}};
}
17 changes: 17 additions & 0 deletions tests/ui/proc-macro/valid-sugg-issue-137345.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ aux-crate: quote=quote-issue-137345.rs

extern crate proc_macro;
extern crate quote;

use proc_macro::TokenStream;

pub fn default_args_fn(_: TokenStream) -> TokenStream {
let decl_macro = TokenStream::new();

quote::quote! {
#(#decl_macro)*
}
.into() //~^^^ ERROR no method named `map` found for opaque type
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/proc-macro/valid-sugg-issue-137345.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0599]: no method named `map` found for opaque type `impl IntoIterator<Item = i32>` in the current scope
--> $DIR/valid-sugg-issue-137345.rs:11:5
|
LL | / quote::quote! {
LL | | #(#decl_macro)*
LL | | }
| |_____^ `impl IntoIterator<Item = i32>` is not an iterator
|
= note: this error originates in the macro `quote::quote` (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 E0599`.

0 comments on commit e780b89

Please sign in to comment.