Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 21, 2020
1 parent 67f319c commit 8fc782a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
Res::SelfTy(None, Some((did, _))) => {
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) {
// NOTE: This path is currently unreachable as `Ty<'tcx>` is
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
// is not actually allowed.
//
// I(@lcnr) still kept this branch in so we don't miss this
// if we ever change it in the future.
return Some(format!("Ty<{}>", substs[0]));
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
return Some(format!("TyCtxt<{}>", substs[0]));
Expand Down
33 changes: 33 additions & 0 deletions src/test/ui-fulldeps/internal-lints/pass_ty_by_ref_self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// NOTE: This test doesn't actually require `fulldeps`
// so we could instead use it as an `ui` test.
//
// Considering that all other `internal-lints` are tested here
// this seems like the cleaner solution though.
#![feature(rustc_attrs)]
#![deny(rustc::ty_pass_by_reference)]
#![allow(unused)]

#[rustc_diagnostic_item = "TyCtxt"]
struct TyCtxt<'tcx> {
inner: &'tcx (),
}

impl<'tcx> TyCtxt<'tcx> {
fn by_value(self) {} // OK
fn by_ref(&self) {} //~ ERROR passing `TyCtxt<'tcx>` by reference
}


struct TyS<'tcx> {
inner: &'tcx (),
}

#[rustc_diagnostic_item = "Ty"]
type Ty<'tcx> = &'tcx TyS<'tcx>;

impl<'tcx> TyS<'tcx> {
fn by_value(self: Ty<'tcx>) {}
fn by_ref(self: &Ty<'tcx>) {} //~ ERROR passing `Ty<'tcx>` by reference
}

fn main() {}
20 changes: 20 additions & 0 deletions src/test/ui-fulldeps/internal-lints/pass_ty_by_ref_self.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: passing `TyCtxt<'tcx>` by reference
--> $DIR/pass_ty_by_ref_self.rs:17:15
|
LL | fn by_ref(&self) {}
| ^^^^^ help: try passing by value: `TyCtxt<'tcx>`
|
note: the lint level is defined here
--> $DIR/pass_ty_by_ref_self.rs:7:9
|
LL | #![deny(rustc::ty_pass_by_reference)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: passing `Ty<'tcx>` by reference
--> $DIR/pass_ty_by_ref_self.rs:30:21
|
LL | fn by_ref(self: &Ty<'tcx>) {}
| ^^^^^^^^^ help: try passing by value: `Ty<'tcx>`

error: aborting due to 2 previous errors

0 comments on commit 8fc782a

Please sign in to comment.