Skip to content

Commit

Permalink
delegation: Do not crash on qpaths without a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 22, 2024
1 parent ac47dba commit 0a26595
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ expand_feature_removed =
expand_glob_delegation_outside_impls =
glob delegation is only supported in impls
expand_glob_delegation_traitless_qpath =
qualified path without a trait in glob delegation
expand_helper_attribute_name_invalid =
`{$name}` cannot be a name of derive helper attribute
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ pub(crate) struct GlobDelegationOutsideImpls {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_glob_delegation_traitless_qpath)]
pub(crate) struct GlobDelegationTraitlessQpath {
#[primary_span]
pub span: Span,
}

// This used to be the `proc_macro_back_compat` lint (#83125). It was later
// turned into a hard error.
#[derive(Diagnostic)]
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::base::*;
use crate::config::StripUnconfigured;
use crate::errors::{
EmptyDelegationMac, GlobDelegationOutsideImpls, IncompleteParse, RecursionLimitReached,
RemoveExprNotSupported, RemoveNodeNotSupported, UnsupportedKeyValue, WrongFragmentKind,
EmptyDelegationMac, GlobDelegationOutsideImpls, GlobDelegationTraitlessQpath, IncompleteParse,
RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported, UnsupportedKeyValue,
WrongFragmentKind,
};
use crate::mbe::diagnostics::annotate_err_with_kind;
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
Expand Down Expand Up @@ -1989,6 +1990,8 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}
None if let Some((deleg, item)) = node.delegation() => {
let Some(suffixes) = &deleg.suffixes else {
let traitless_qself =
matches!(&deleg.qself, Some(qself) if qself.position == 0);
let item = match node.to_annotatable() {
Annotatable::ImplItem(item) => item,
ann @ (Annotatable::Item(_)
Expand All @@ -2000,6 +2003,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}
_ => unreachable!(),
};
if traitless_qself {
let span = item.span;
self.cx.dcx().emit_err(GlobDelegationTraitlessQpath { span });
return Default::default();
}
return self.collect_glob_delegation(item, Node::KIND).make_ast::<Node>();
};

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/delegation/glob-traitless-qpath.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(fn_delegation)]
#![allow(incomplete_features)]

struct S;

impl S {
reuse <u8>::*; //~ ERROR qualified path without a trait in glob delegation
reuse <()>::*; //~ ERROR qualified path without a trait in glob delegation
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/delegation/glob-traitless-qpath.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: qualified path without a trait in glob delegation
--> $DIR/glob-traitless-qpath.rs:7:5
|
LL | reuse <u8>::*;
| ^^^^^^^^^^^^^^

error: qualified path without a trait in glob delegation
--> $DIR/glob-traitless-qpath.rs:8:5
|
LL | reuse <()>::*;
| ^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

0 comments on commit 0a26595

Please sign in to comment.