Skip to content
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

lint: add internal mutability lint. #14241

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 28 additions & 0 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub enum Lint {
TypeOverflow,
UnusedUnsafe,
UnsafeBlock,
InternalMutability,
AttributeUsage,
UnknownFeatures,
UnknownCrateType,
Expand Down Expand Up @@ -281,6 +282,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
default: allow
}),

("internal_mutability",
LintSpec {
lint: InternalMutability,
desc: "detect the use of types with internal (non-inherited) mutability",
default: allow
}),

("attribute_usage",
LintSpec {
lint: AttributeUsage,
Expand Down Expand Up @@ -1387,6 +1395,25 @@ fn check_unsafe_block(cx: &Context, e: &ast::Expr) {
}
}

fn check_internal_mutability(cx: &Context, e: &ast::Expr) {
let (name, args) = match e.node {
ast::ExprMethodCall(_, _, ref args) => ("method", args),
ast::ExprCall(_, ref args) => ("function", args),
_ => return
};

for arg in args.iter() {
let arg_ty = ty::expr_ty(cx.tcx, *arg);
let reaches_unsafe = ty::type_interior_reaches_unsafe(cx.tcx, arg_ty);

if reaches_unsafe {
cx.span_lint(InternalMutability, arg.span,
format_strbuf!("internally mutable type used in {} call",
name).as_slice())
}
}
}

fn check_unused_mut_pat(cx: &Context, pats: &[@ast::Pat]) {
// collect all mutable pattern and group their NodeIDs by their Identifier to
// avoid false warnings in match arms with multiple patterns
Expand Down Expand Up @@ -1742,6 +1769,7 @@ impl<'a> Visitor<()> for Context<'a> {
check_unnecessary_parens_expr(self, e);
check_unused_unsafe(self, e);
check_unsafe_block(self, e);
check_internal_mutability(self, e);
check_unnecessary_allocation(self, e);
check_heap_expr(self, e);

Expand Down
14 changes: 11 additions & 3 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1811,13 +1811,14 @@ def_type_content_sets!(
OwnsAffine = 0b0000_0000__0000_1000__0000,
OwnsAll = 0b0000_0000__1111_1111__0000,

// Things that are reachable by the value in any way (fourth nibble):
// Things that are reachable by the value in any way (fourth nibble (and more)):
ReachesNonsendAnnot = 0b0000_0001__0000_0000__0000,
ReachesBorrowed = 0b0000_0010__0000_0000__0000,
// ReachesManaged /* see [1] below */ = 0b0000_0100__0000_0000__0000,
ReachesMutable = 0b0000_1000__0000_0000__0000,
ReachesNoShare = 0b0001_0000__0000_0000__0000,
ReachesAll = 0b0001_1111__0000_0000__0000,
ReachesUnsafe = 0b0010_0000__0000_0000__0000,
ReachesAll = 0b0011_1111__0000_0000__0000,

// Things that cause values to *move* rather than *copy*
Moves = 0b0000_0000__0000_1011__0000,
Expand Down Expand Up @@ -1911,6 +1912,10 @@ impl TypeContents {
self.intersects(TC::InteriorUnsafe)
}

pub fn reaches_unsafe(&self) -> bool {
self.intersects(TC::ReachesUnsafe)
}

pub fn interior_unsized(&self) -> bool {
self.intersects(TC::InteriorUnsized)
}
Expand Down Expand Up @@ -2002,6 +2007,9 @@ pub fn type_is_sendable(cx: &ctxt, t: ty::t) -> bool {
pub fn type_interior_is_unsafe(cx: &ctxt, t: ty::t) -> bool {
type_contents(cx, t).interior_unsafe()
}
pub fn type_interior_reaches_unsafe(cx: &ctxt, t: ty::t) -> bool {
type_contents(cx, t).reaches_unsafe()
}

pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
let ty_id = type_id(ty);
Expand Down Expand Up @@ -2186,7 +2194,7 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
} else if Some(did) == cx.lang_items.unsafe_type() {
// FIXME(#13231): This shouldn't be needed after
// opt-in built-in bounds are implemented.
(tc | TC::InteriorUnsafe) - TC::Nonsharable
(tc | TC::ReachesUnsafe | TC::InteriorUnsafe) - TC::Nonsharable
} else {
tc
}
Expand Down
65 changes: 65 additions & 0 deletions src/test/compile-fail/lint-internal-mutability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![deny(internal_mutability)]

use std::cell::{Cell,RefCell};
use std::ty::Unsafe;
use std::rc::Rc;

struct Foo {
x: Unsafe<uint>
}
impl Foo {
fn amp_call(&self) {}
fn amp_mut_call(&mut self) {}
fn call(self) {}
}

struct Bar;

impl Bar {
fn call(&self, _: Cell<uint>, _: &RefCell<uint>, _: &Rc<uint>) {}
fn call2(&self, _: &Cell<uint>, _: RefCell<uint>, _: &uint) {}
}

fn call(_: Cell<uint>, _: &RefCell<uint>, _: &Rc<uint>) {}
fn call2(_: &Cell<uint>, _: RefCell<uint>, _: &uint) {}


fn main() {
let a = Cell::new(1);
let b1 = RefCell::new(2u);
let b2 = b1.clone(); //~ ERROR internally mutable type used in method call
let c = Rc::new(3);

call(a, //~ ERROR internally mutable type used in function call
&b1, //~ ERROR internally mutable type used in function call
&c); //~ ERROR internally mutable type used in function call

call2(&a, //~ ERROR internally mutable type used in function call
b1, //~ ERROR internally mutable type used in function call
&*c);

let mut foo = Foo { x: Unsafe::new(4) };

foo.amp_call();//~ ERROR internally mutable type used in method call
foo.amp_mut_call();//~ ERROR internally mutable type used in method call
foo.call();//~ ERROR internally mutable type used in method call

Bar.call(a, //~ ERROR internally mutable type used in method call
&b2, //~ ERROR internally mutable type used in method call
&c); //~ ERROR internally mutable type used in method call

Bar.call2(&a, //~ ERROR internally mutable type used in method call
b2, //~ ERROR internally mutable type used in method call
&*c);

}