]> Cypherpunks repositories - gostls13.git/commitdiff
gc: change -u to require imports to be marked safe
authorRuss Cox <rsc@golang.org>
Fri, 11 Jun 2010 22:28:43 +0000 (15:28 -0700)
committerRuss Cox <rsc@golang.org>
Fri, 11 Jun 2010 22:28:43 +0000 (15:28 -0700)
R=ken2
CC=golang-dev
https://golang.org/cl/1597043

src/cmd/gc/export.c
src/cmd/gc/go.h
src/cmd/gc/go.y
src/cmd/gc/lex.c
src/cmd/gc/typecheck.c

index 9992c5219e8afacb09b40dff94c66035fd125cb9..c73c476b6ec171bc4eee3711c4a30a8d3f8cf353 100644 (file)
@@ -268,7 +268,10 @@ dumpexport(void)
        packagequotes = 1;
        Bprint(bout, "\n$$  // exports\n");
 
-       Bprint(bout, "    package %s\n", localpkg->name);
+       Bprint(bout, "    package %s", localpkg->name);
+       if(safemode)
+               Bprint(bout, " safe");
+       Bprint(bout, "\n");
 
        for(l=exportlist; l; l=l->next) {
                lineno = l->n->lineno;
index 2cf408e760025aedb49427810259d7c4c79c21cc..2f63ba40f07d936d1d0118cf85919054e88bfed9 100644 (file)
@@ -578,6 +578,7 @@ struct      Io
        int     peekc;
        int     peekc1; // second peekc for ...
        char*   cp;     // used for content when bin==nil
+       int     importsafe;
 };
 
 typedef        struct  Dlist   Dlist;
index c7a1f111bcc837875f1daa0dd0daba698c09fddc..2c4623f15cee65e95339964c24ec8c91ad604301 100644 (file)
@@ -152,6 +152,7 @@ loadsys:
                        cannedimports("runtime.builtin", "package runtime\n\n$$\n\n");
                else
                        cannedimports("runtime.builtin", runtimeimport);
+               curio.importsafe = 1;
        }
        import_package
        import_there
@@ -236,10 +237,13 @@ import_here:
        }
 
 import_package:
-       LPACKAGE sym ';'
+       LPACKAGE sym import_safety ';'
        {
                importpkg->name = $2->name;
                importpkg->direct = 1;
+               
+               if(safemode && !curio.importsafe)
+                       yyerror("cannot import unsafe package %Z", importpkg->path);
 
                // NOTE(rsc): This is no longer a technical restriction:
                // the 6g tool chain would work just fine without giving
@@ -250,6 +254,13 @@ import_package:
                        yyerror("cannot import package main");
        }
 
+import_safety:
+|      LNAME
+       {
+               if(strcmp($1->name, "safe") == 0)
+                       curio.importsafe = 1;
+       }
+
 import_there:
        {
                defercheckwidth();
index 5dc6d78cfe6381030086c6ca8d72ee661907b749..b08100993cf0b6e629c38959cba335860484ce9f 100644 (file)
@@ -442,6 +442,7 @@ cannedimports(char *file, char *cp)
        curio.infile = file;
        curio.cp = cp;
        curio.nlsemi = 0;
+       curio.importsafe = 0;
 
        typecheckok = 1;
        incannedimport = 1;
index 592166c885e2effd63476efbfb9e055a63141001..70aa3cb9d15b28cb58d7bf7afebd6cbd8bc1dee9 100644 (file)
@@ -1191,7 +1191,10 @@ ret:
                        checkwidth(t);
                }
        }
-       if(safemode && isptrto(t, TANY))
+
+       // TODO(rsc): should not need to check importpkg,
+       // but reflect mentions unsafe.Pointer.
+       if(safemode && !incannedimport && !importpkg && isptrto(t, TANY))
                yyerror("cannot use unsafe.Pointer");
 
        evconst(n);