Skip to content

Commit

Permalink
Rollup merge of rust-lang#132207 - compiler-errors:tweak-res-mod-segm…
Browse files Browse the repository at this point in the history
…ent, r=petrochenkov

Store resolution for self and crate root module segments

Let's make sure to record the segment resolution for `self::`, `crate::` and `$crate::`.

I'm actually somewhat surprised that the only diagnostic that uses this is the one that errors on invalid generics on a module segment... but seems strictly more correct regardless, and there may be other diagnostics using these segments resolutions that just haven't been tested for `self`. Also includes a drive-by on `report_prohibit_generics_error`.
  • Loading branch information
matthiaskrgr authored Nov 20, 2024
2 parents ce1543f + ba12a99 commit edf0995
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.collect::<String>()
),
[(only, _)] => only.to_string(),
[] => "this type".to_string(),
[] => bug!("expected one segment to deny"),
};

let arg_spans: Vec<Span> = segments
Expand Down Expand Up @@ -1136,7 +1136,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
"s",
),
[only] => (only.to_string(), ""),
[] => unreachable!("expected at least one generic to prohibit"),
[] => bug!("expected at least one generic to prohibit"),
};
let last_span = *arg_spans.last().unwrap();
let span: MultiSpan = arg_spans.into();
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,9 +1478,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
if segment_idx == 0 {
if name == kw::SelfLower {
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
module = Some(ModuleOrUniformRoot::Module(
self.resolve_self(&mut ctxt, parent_scope.module),
));
let self_mod = self.resolve_self(&mut ctxt, parent_scope.module);
if let Some(res) = self_mod.res() {
record_segment_res(self, res);
}
module = Some(ModuleOrUniformRoot::Module(self_mod));
continue;
}
if name == kw::PathRoot && ident.span.at_least_rust_2018() {
Expand All @@ -1497,7 +1499,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
if name == kw::PathRoot || name == kw::Crate || name == kw::DollarCrate {
// `::a::b`, `crate::a::b` or `$crate::a::b`
module = Some(ModuleOrUniformRoot::Module(self.resolve_crate_root(ident)));
let crate_root = self.resolve_crate_root(ident);
if let Some(res) = crate_root.res() {
record_segment_res(self, res);
}
module = Some(ModuleOrUniformRoot::Module(crate_root));
continue;
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/generics/generics-on-self-mod-segment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct Ty;

fn self_(_: self::<i32>::Ty) {}
//~^ ERROR type arguments are not allowed on module `generics_on_self_mod_segment`

fn crate_(_: crate::<i32>::Ty) {}
//~^ ERROR type arguments are not allowed on module `generics_on_self_mod_segment`

macro_rules! dollar_crate {
() => {
fn dollar_crate_(_: $crate::<i32>::Ty) {}
//~^ ERROR type arguments are not allowed on module `generics_on_self_mod_segment`
}
}

dollar_crate!();

fn main() {}
32 changes: 32 additions & 0 deletions tests/ui/generics/generics-on-self-mod-segment.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error[E0109]: type arguments are not allowed on module `generics_on_self_mod_segment`
--> $DIR/generics-on-self-mod-segment.rs:3:20
|
LL | fn self_(_: self::<i32>::Ty) {}
| ---- ^^^ type argument not allowed
| |
| not allowed on module `generics_on_self_mod_segment`

error[E0109]: type arguments are not allowed on module `generics_on_self_mod_segment`
--> $DIR/generics-on-self-mod-segment.rs:6:22
|
LL | fn crate_(_: crate::<i32>::Ty) {}
| ----- ^^^ type argument not allowed
| |
| not allowed on module `generics_on_self_mod_segment`

error[E0109]: type arguments are not allowed on module `generics_on_self_mod_segment`
--> $DIR/generics-on-self-mod-segment.rs:11:38
|
LL | fn dollar_crate_(_: $crate::<i32>::Ty) {}
| ------ ^^^ type argument not allowed
| |
| not allowed on module `generics_on_self_mod_segment`
...
LL | dollar_crate!();
| --------------- in this macro invocation
|
= note: this error originates in the macro `dollar_crate` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0109`.

0 comments on commit edf0995

Please sign in to comment.