summaryrefslogtreecommitdiffstats
path: root/tools/test-surface.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/test-surface.py')
-rw-r--r--tools/test-surface.py56
1 files changed, 55 insertions, 1 deletions
diff --git a/tools/test-surface.py b/tools/test-surface.py
index b21133a..6121889 100644
--- a/tools/test-surface.py
+++ b/tools/test-surface.py
@@ -182,6 +182,59 @@ def gated_declarations(path):
return out
+def aliased_behind_private_module(path, name):
+ """True when the only public path to `name` is a gated `pub use` alias.
+
+ The inner-module narrowing declares the item `pub` inside a module nobody
+ outside can name, then re-exports it under a feature gate:
+
+ mod kata_backend { pub struct KataBackend { ... } }
+ #[cfg(feature = "test-support")] pub use kata_backend::KataBackend;
+ #[cfg(not(feature = "test-support"))] pub(crate) use kata_backend::KataBackend;
+
+ The census sees the inner `pub` and calls it a public declaration. It is
+ not one: a `pub` item in a private module has no nameable path, and the
+ alias that gives it one is gated. Both halves must hold for this to be true.
+ """
+ lines = open(path).read().split("\n")
+
+ # 1. is there a `pub use ...::NAME;` whose attribute block carries the gate?
+ aliased = False
+ for i, line in enumerate(lines):
+ if not re.search(r"^\s*pub\s+use\s+[^;]*::%s\b" % re.escape(name), line):
+ continue
+ k = i - 1
+ while k >= i - 8 and (lines[k].strip().startswith("#[")
+ or lines[k].strip().startswith("//")
+ or not lines[k].strip()):
+ if GATE in lines[k]:
+ aliased = True
+ k -= 1
+ if not aliased:
+ return False
+
+ # 2. does every `pub` declaration of the name sit inside a private module?
+ decls = [i for i, l in enumerate(lines)
+ if re.match(r"^\s*pub\s+(?:fn|struct|enum|trait|const|static|type)\s+%s\b"
+ % re.escape(name), l)]
+ if not decls:
+ return False
+ for d in decls:
+ depth, enclosed = 0, []
+ for k in range(d - 1, -1, -1):
+ # Walking backwards, an unmatched `{` means that line opens a block
+ # we are inside of.
+ depth += lines[k].count("{") - lines[k].count("}")
+ if depth > 0:
+ m = re.match(r"^\s*(pub(?:\(crate\))?\s+)?mod\s+(\w+)", lines[k])
+ if m:
+ enclosed.append(m.group(1))
+ depth -= 1
+ if not enclosed or any(enc for enc in enclosed):
+ return False
+ return True
+
+
def integration_tests(path):
lines = open(path).read().split("\n")
starts = [i for i, l in enumerate(lines) if l.lstrip().startswith("#[test]")]
@@ -220,7 +273,8 @@ def analyse():
occ = [(os.path.relpath(p, ROOT), ln, g)
for p in paths for (ln, g) in gated_declarations(p).get(name, [])]
- gated = any(g for _, _, g in occ)
+ gated = any(g for _, _, g in occ) or any(
+ aliased_behind_private_module(p, name) for p in paths)
# A mention inside a string literal is not a call.
string_only = all(