Skip to content

Don't give APITs names with macro expansion placeholder fragments in it #142393

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 1 commit 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
4 changes: 4 additions & 0 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,10 @@ pub trait ResolverExpand {
trait_def_id: DefId,
impl_def_id: LocalDefId,
) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate>;

/// Record the name of an opaque `Ty::ImplTrait` pre-expansion so that it can be used
/// to generate an item name later that does not reference placeholder macros.
fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
}

pub trait LintStoreExpand {
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS};
use rustc_session::parse::feature_err;
use rustc_session::{Limit, Session};
use rustc_span::hygiene::SyntaxContext;
use rustc_span::{ErrorGuaranteed, FileName, Ident, LocalExpnId, Span, sym};
use rustc_span::{ErrorGuaranteed, FileName, Ident, LocalExpnId, Span, Symbol, sym};
use smallvec::SmallVec;

use crate::base::*;
Expand Down Expand Up @@ -2315,7 +2315,17 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}

fn visit_ty(&mut self, node: &mut P<ast::Ty>) {
self.visit_node(node)
// Save the pre-expanded name of this `ImplTrait`, so that later when defining
// an APIT we use a name that doesn't have any placeholder fragments in it.
if let ast::TyKind::ImplTrait(id, _) = &mut node.kind {
// HACK: Assign an ID to this node out-of-order.
self.visit_id(id);
let id = *id;
let name = Symbol::intern(&pprust::ty_to_string(node).replace('\n', " "));
self.cx.resolver.insert_impl_trait_name(id, name);
}

self.visit_node(node);
}

fn visit_pat(&mut self, node: &mut P<ast::Pat>) {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::mem;

use rustc_ast::visit::FnKind;
use rustc_ast::*;
use rustc_ast_pretty::pprust;
use rustc_attr_parsing::{AttributeParser, OmitDoc};
use rustc_expand::expand::AstFragment;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::span_bug;
use rustc_span::hygiene::LocalExpnId;
use rustc_span::{Span, Symbol, sym};
use tracing::debug;
Expand Down Expand Up @@ -381,7 +381,11 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
// gets too long. We don't want these to show up in compiler
// output or built artifacts, so replace them here...
// Perhaps we should instead format APITs more robustly.
let name = Symbol::intern(&pprust::ty_to_string(ty).replace('\n', " "));
let name = *self
.resolver
.impl_trait_names
.get(id)
.unwrap_or_else(|| span_bug!(ty.span, "expected this opaque to be named"));
let kind = match self.invocation_parent.impl_trait_context {
ImplTraitContext::Universal => DefKind::TyParam,
ImplTraitContext::Existential => DefKind::OpaqueTy,
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,11 @@ pub struct Resolver<'ra, 'tcx> {
current_crate_outer_attr_insert_span: Span,

mods_with_parse_errors: FxHashSet<DefId>,

// Stores pre-expansion and pre-placeholder-fragment-insertion names for `impl Trait` types
// that were encountered during resolution. These names are used to generate item names
// for APITs, so we don't want to leak details of resolution into these names.
impl_trait_names: FxHashMap<NodeId, Symbol>,
}

/// This provides memory for the rest of the crate. The `'ra` lifetime that is
Expand Down Expand Up @@ -1579,6 +1584,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
impl_binding_keys: Default::default(),
current_crate_outer_attr_insert_span,
mods_with_parse_errors: Default::default(),
impl_trait_names: Default::default(),
};

let root_parent_scope = ParentScope::module(graph_root, &resolver);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
});
Ok(idents)
}

fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol) {
self.impl_trait_names.insert(id, name);
}
}

impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Expand Down
9 changes: 0 additions & 9 deletions tests/crashes/140333.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ui/impl-trait/name-mentioning-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Foo<T> {}

macro_rules! bar {
() => { () }
}

fn foo(x: impl Foo<bar!()>) {
let () = x;
//~^ ERROR mismatched types
}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/impl-trait/name-mentioning-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/name-mentioning-macro.rs:8:9
|
LL | fn foo(x: impl Foo<bar!()>) {
| ---------------- expected this type parameter
LL | let () = x;
| ^^ - this expression has type `impl Foo<bar!()>`
| |
| expected type parameter `impl Foo<bar!()>`, found `()`
Copy link
Member Author

@compiler-errors compiler-errors Jun 12, 2025

Choose a reason for hiding this comment

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

For contrast, this used to render like impl Foo<!()>, where !() is the placeholder fragment.

|
= note: expected type parameter `impl Foo<bar!()>`
found unit type `()`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
16 changes: 16 additions & 0 deletions tests/ui/impl-trait/struct-field-fragment-in-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ check-pass

trait Trait<T> {}

fn a(_: impl Trait<
[(); {
struct D {
#[rustfmt::skip]
bar: (),
}
0
}],
>) {
}

fn main() {}
Loading