Skip to content

Commit 8fc782a

Browse files
committed
add test
1 parent 67f319c commit 8fc782a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

compiler/rustc_lint/src/internal.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
191191
Res::SelfTy(None, Some((did, _))) => {
192192
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
193193
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) {
194+
// NOTE: This path is currently unreachable as `Ty<'tcx>` is
195+
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
196+
// is not actually allowed.
197+
//
198+
// I(@lcnr) still kept this branch in so we don't miss this
199+
// if we ever change it in the future.
194200
return Some(format!("Ty<{}>", substs[0]));
195201
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
196202
return Some(format!("TyCtxt<{}>", substs[0]));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// NOTE: This test doesn't actually require `fulldeps`
2+
// so we could instead use it as an `ui` test.
3+
//
4+
// Considering that all other `internal-lints` are tested here
5+
// this seems like the cleaner solution though.
6+
#![feature(rustc_attrs)]
7+
#![deny(rustc::ty_pass_by_reference)]
8+
#![allow(unused)]
9+
10+
#[rustc_diagnostic_item = "TyCtxt"]
11+
struct TyCtxt<'tcx> {
12+
inner: &'tcx (),
13+
}
14+
15+
impl<'tcx> TyCtxt<'tcx> {
16+
fn by_value(self) {} // OK
17+
fn by_ref(&self) {} //~ ERROR passing `TyCtxt<'tcx>` by reference
18+
}
19+
20+
21+
struct TyS<'tcx> {
22+
inner: &'tcx (),
23+
}
24+
25+
#[rustc_diagnostic_item = "Ty"]
26+
type Ty<'tcx> = &'tcx TyS<'tcx>;
27+
28+
impl<'tcx> TyS<'tcx> {
29+
fn by_value(self: Ty<'tcx>) {}
30+
fn by_ref(self: &Ty<'tcx>) {} //~ ERROR passing `Ty<'tcx>` by reference
31+
}
32+
33+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: passing `TyCtxt<'tcx>` by reference
2+
--> $DIR/pass_ty_by_ref_self.rs:17:15
3+
|
4+
LL | fn by_ref(&self) {}
5+
| ^^^^^ help: try passing by value: `TyCtxt<'tcx>`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/pass_ty_by_ref_self.rs:7:9
9+
|
10+
LL | #![deny(rustc::ty_pass_by_reference)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: passing `Ty<'tcx>` by reference
14+
--> $DIR/pass_ty_by_ref_self.rs:30:21
15+
|
16+
LL | fn by_ref(self: &Ty<'tcx>) {}
17+
| ^^^^^^^^^ help: try passing by value: `Ty<'tcx>`
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)